The execution of accompanying messages cannot be automatically divided between processes, because a coroutine is executed inside a certain event loop in a process that belongs to the async class. There is a state in coroutine that cannot be selected, and even if it were possible, it would not make sense outside the context of its event loop.
What you can do is create an adapter based on the callback for your async class, with each coroutine method represented by a callback method with the semantics of “start making X and call this function upon completion”. If the callback is multiprocessor, these operations can be called from other processes. Then you can start the event loop in each process and create the look of the coroutine code on top of proxied calls based on the callback.
For example, consider the trivial async class:
class Async: async def repeat(self, n, s): for i in range(n): print(s, i, os.getpid()) await asyncio.sleep(.2) return s
The adapter with the callback can use the asyncio public API to convert the repeat coroutine into a classic asynchronous JavaScript-style callback hell function:
class CallbackAdapter: def repeat_start(self, n, s, on_success): fut = asyncio.run_coroutine_threadsafe( self._async.repeat(n, s), self._loop)
(The conversion can be automated, the above hand-written code just shows the concept.)
CallbackAdapter can be registered using multiprocessing, so different processes can start the adapter method (and, therefore, the original asynchronous coroutine) using proxies that provide multiprocessing. It only requires that the callback be passed as on_success for multiprocessing.
As a last step, you could complete a full circle and create an asynchronous adapter for the callback API (!), Start an event loop in another process, and use asyncio and async def . This adapter class for the adapter will have a full-function repeat coroutine that Async.repeat original Async.repeat coroutine Async.repeat without even trying to sort the coroutine state.
The following is an example implementation of the above approach:
import asyncio, multiprocessing.managers, threading, os class Async:
The code works correctly on Python 3.5, but does not work on 3.6 and 3.7 due to an error in multiprocessing .