Question about asyncio and blocking operations
Frank Millman
frank at chagford.com
Thu Jan 28 01:18:55 EST 2016
"Ian Kelly" wrote in message
news:CALwzidn6TvN9W-2qnn2JYvJu8NHzn499nPtfjn9OHjdDcebVbA at mail.gmail.com...
> On Wed, Jan 27, 2016 at 7:40 AM, Frank Millman <frank at chagford.com> wrote:
> >
> > Assume a slow function -
> >
> > async def slow_function(arg1, arg2):
> > [do stuff]
> >
> > It now looks like this -
> >
> > async def slow_function(arg1, arg2):
> > loop = asyncio.get_event_loop()
> > await loop.run_in_executor(None, slow_function_1, arg1, arg2)
> >
> > def slow_function_1(self, arg1, arg2):
> > loop = asyncio.new_event_loop()
> > asyncio.set_event_loop(loop)
> > loop.run_until_complete(slow_function_2(arg1, arg2))
> >
> > async slow_function_2(arg1, arg2):
> > [do stuff]
> >
> > Does this look right?
>
> I'm not sure I understand what you're trying to accomplish by running
> a second event loop inside the executor thread. It will only be useful
> for scheduling asynchronous operations, and if they're asynchronous
> then why not schedule them on the original event loop?
I could be confusing myself here, but this is what I am trying to do.
run_in_executor() schedules a blocking function to run in the executor, and
returns a Future.
If you just invoke it, the blocking function will execute in the background,
and the calling function will carry on.
If you obtain a reference to the Future, and then 'await' it, the calling
function will be suspended until the blocking function is complete. You
might do this because you want the calling function to block, but you do not
want to block the entire event loop.
In the above example, I do not want the calling function to block. However,
the blocking function invokes one or more coroutines, so it needs an event
loop to operate. Creating a new event loop allows them to run independently.
Hope this makes sense.
Frank
More information about the Python-list
mailing list