Issue
I have a service implemented in Python 3.5 that should load a file over the network periodically. I would like to avoid blocking the main thread while it loads. In order to avoid introducing the complexity of threads and shared memory to my application, I wanted to use an event loop with the asyncio package to instrument this.
To run an event loop, I've found the AbstractEventLoop.run_forever()
and AbstractEventLoop.run_until_complete(future)
methods, but both seem to be blocking the main thread when called. The only way I've found to avoid this is to start the loop in a different thread. But then if I'm using threads anyway, there is no point in using an event loop.
So my question is: Is it possible to start an asyncio event loop in the background without spawning a thread or process?
Solution
You can use the run_in_executor
method. Each of the functions you run with this method run in their own thread (in parallel).
The AbstractEventLoop.run_in_executor() method can be used with a thread pool executor to execute a callback in different thread to not block the thread of the event loop.
The executor argument should be an Executor instance. The default executor is used if executor is None.
For example:
import asyncio
def load_file_over_network(file_name):
# do the file loading
pass
loop = asyncio.get_event_loop()
file_name = 'FILE_NAME'
# load the file without blocking
loop.run_in_executor(None, load_file_over_network, file_name)
# do some other stuff in the main thread
# while the synchronous code above is running in other threads, the event loop
# can go do other things
# load the file again without blocking
loop.run_in_executor(None, load_file_over_network, file_name)
Hope this was helpful :)
Answered By - Yuval Pruss
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.