Kotlin Coroutines the right way in Android

After struggling with this question for days, I think the most simple and clear async-await pattern for Android activities using Kotlin is: override fun onCreate(savedInstanceState: Bundle?) { //… loadDataAsync(); //”Fire-and-forget” } fun loadDataAsync() = async(UI) { try { //Turn on busy indicator. val job = async(CommonPool) { //We’re on a background thread here. //Execute blocking … Read more

Test if function or method is normal or asynchronous

Use the inspect module of Python. inspect.iscoroutinefunction(object) Return true if the object is a coroutine function (a function defined with an async def syntax). This function is available since Python 3.5. The module is available for Python 2 with lesser functionalities and certainly without the one you are looking for: inspect Inspect module as the … Read more

How to use code that relies on ThreadLocal with Kotlin coroutines

Coroutine’s analog to ThreadLocal is CoroutineContext. To interoperate with ThreadLocal-using libraries you need to implement a custom ContinuationInterceptor that supports framework-specific thread-locals. Here is an example. Let us assume that we use some framework that relies on a specific ThreadLocal to store some application-specific data (MyData in this example): val myThreadLocal = ThreadLocal<MyData>() To use … Read more

What is the purpose of coroutine yield()?

https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/yield.html Yields a thread (or thread pool) of the current coroutine dispatcher to other coroutines to run. If the coroutine dispatcher does not have its own thread pool (like Dispatchers.Unconfined) then this function does nothing, but checks if the coroutine Job was completed. This suspending function is cancellable. If the Job of the current coroutine … Read more

Duplication of code for synchronous and asynchronous implementations

There is no one-size-fits-all road to making an asyncio coroutine-based codebase useable from traditional synchronous codebases. You have to make choices per codepath. Pick and choose from a series of tools: Synchronous versions using asyncio.run() Provide synchronous wrappers around coroutines, which block until the coroutine completes. Even an async generator function such as ticker() can … Read more

Is it safe to yield from within a “with” block in Python (and why)?

I don’t really understand what conflict you’re asking about, nor the problem with the example: it’s fine to have two coexisting, independent handles to the same file. One thing I didn’t know that I learned in response to your question it that there is a new close() method on generators: close() raises a new GeneratorExit … Read more

Coroutines in C#

I believe with the new .NET 4.5\C# 5 the async\await pattern should meet your needs. async Task<string> DownloadDocument(Uri uri) { var webClient = new WebClient(); var doc = await webClient.DownloadStringTaskAsync(url); // do some more async work return doc; } I suggest looking at http://channel9.msdn.com/Events/TechEd/Australia/Tech-Ed-Australia-2011/DEV411 for more info. It is a great presentation. Also http://msdn.microsoft.com/en-us/vstudio/gg316360 has … Read more

How to use async/await in Python 3.5?

Running coroutines requires an event loop. Use the asyncio() library to create one: import asyncio # Python 3.7+ asyncio.run(foo()) or # Python 3.6 and older loop = asyncio.get_event_loop() loop.run_until_complete(foo()) Also see the Tasks and Coroutines chapter of the asyncio documentation. If you already have a loop running, you’d want to run additional coroutines concurrently by … Read more