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


Groups > comp.lang.python > #102173

Re: Question about asyncio and blocking operations

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: Question about asyncio and blocking operations
Date Thu, 28 Jan 2016 08:18:55 +0200
Lines 53
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>
Mime-Version 1.0
Content-Type text/plain; format=flowed; charset="iso-8859-1"; reply-type=original
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de L6keBTlaLTtJzgoZKEZrlATkUmOuMUgDiVn4sqNUPdMw==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:Question': 0.05; 'block.': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'assume': 0.11; 'jan': 0.11; 'def': 0.13; 'wed,': 0.15; '2016': 0.16; 'async': 0.16; 'background,': 0.16; 'executor': 0.16; 'invokes': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'skip:n 70': 0.16; 'thread.': 0.16; 'wrote:': 0.16; 'do.': 0.22; 'trying': 0.22; 'am,': 0.23; 'wrote': 0.23; 'skip:l 40': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'header:X-Complaints-To:1': 0.26; 'operations,': 0.27; 'function': 0.28; 'looks': 0.29; 'block,': 0.29; 'blocking': 0.29; 'invoke': 0.29; 'schedules': 0.29; 'allows': 0.30; "i'm": 0.30; 'creating': 0.30; 'run': 0.33; 'useful': 0.33; 'right?': 0.33; 'schedule': 0.34; 'running': 0.34; 'could': 0.35; 'but': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'why': 0.39; 'sure': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'hope': 0.61; 'entire': 0.61; 'more': 0.63; "they're": 0.66; 'future.': 0.67; 'future,': 0.70; 'frank': 0.72; 'await': 0.76; 'arg1,': 0.84; 'confusing': 0.84; 'scheduling': 0.91; 'suspended': 0.91
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 197.89.169.53
In-Reply-To <CALwzidn6TvN9W-2qnn2JYvJu8NHzn499nPtfjn9OHjdDcebVbA@mail.gmail.com>
X-MSMail-Priority Normal
Importance Normal
X-Newsreader Microsoft Windows Live Mail 15.4.3502.922
X-MimeOLE Produced By Microsoft MimeOLE V15.4.3502.922
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:102173

Show key headers only | 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