Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102173

Re: Question about asyncio and blocking operations

From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: Question about asyncio and blocking operations
Date 2016-01-28 08:18 +0200
Message-ID <mailman.43.1453961949.2338.python-list@python.org> (permalink)
References <n8038j$575$1@ger.gmane.org> <n8818q$35e$1@ger.gmane.org> <CALwzidk-RBkB-vi6CgcEeoFHQrsoTFvqX9MqzDD=rnY5bOCRUg@mail.gmail.com> <n8aln3$fah$1@ger.gmane.org> <CALwzidn6TvN9W-2qnn2JYvJu8NHzn499nPtfjn9OHjdDcebVbA@mail.gmail.com>

Show all headers | View raw


"Ian Kelly"  wrote in message 
news:CALwzidn6TvN9W-2qnn2JYvJu8NHzn499nPtfjn9OHjdDcebVbA@mail.gmail.com...

> On Wed, Jan 27, 2016 at 7:40 AM, Frank Millman <frank@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Question about asyncio and blocking operations "Frank Millman" <frank@chagford.com> - 2016-01-28 08:18 +0200

csiph-web