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


Groups > comp.lang.python > #105700 > unrolled thread

Adding run_in_executor task to already existing loop.

Started byRay Cote <rgacote@appropriatesolutions.com>
First post2016-03-25 16:24 -0400
Last post2016-03-25 16:00 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  Adding run_in_executor task to already existing loop. Ray Cote <rgacote@appropriatesolutions.com> - 2016-03-25 16:24 -0400
    Re: Adding run_in_executor task to already existing loop. Marko Rauhamaa <marko@pacujo.net> - 2016-03-25 22:56 +0200
      Re: Adding run_in_executor task to already existing loop. Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-03-25 16:00 -0500

#105700 — Adding run_in_executor task to already existing loop.

FromRay Cote <rgacote@appropriatesolutions.com>
Date2016-03-25 16:24 -0400
SubjectAdding run_in_executor task to already existing loop.
Message-ID<mailman.7.1458937485.28225.python-list@python.org>
Hello:

I’m trying to perform an synchronous task while using asyncio.
I understand the solution is to use run_in_executor.
I’m not clear on how to add this into an already running event loop.

I’ve found lots of examples showing how to set up a loop and run this, but
I’m blocked in regards to doing this when the loop is already established.


Example code:

def blocking_func(param1):
    # call the blocking call here.
    return results

async def process_request():
    loop = asyncio.get_event_loop()
    block = loop.run_in_executor(None, blocking_func, “hello”)
    results = await loop.run_until_complete(asyncio.gather(*[block, ])

The above code says “loop already running.” because we’re already in an
async ask that has been awaited. What is the proper method of adding in
this new synchronous task?

Regards
—Ray
-- 
Raymond Cote, President
voice: +1.603.924.6079 email: rgacote@AppropriateSolutions.com skype:
ray.cote

[toc] | [next] | [standalone]


#105701

FromMarko Rauhamaa <marko@pacujo.net>
Date2016-03-25 22:56 +0200
Message-ID<87mvpm1dlo.fsf@elektro.pacujo.net>
In reply to#105700
Ray Cote <rgacote@appropriatesolutions.com>:

> I’m trying to perform an synchronous task while using asyncio.

You seem to want to do something you shouldn't be doing. Asyncio does
not tolerate synchronous/blocking calls. You will need to move those in
separate threads or processes if you can't turn them into asynchronous
tasks.


Marko

[toc] | [prev] | [next] | [standalone]


#105703

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2016-03-25 16:00 -0500
Message-ID<mailman.9.1458939670.28225.python-list@python.org>
In reply to#105701
On Fri, Mar 25, 2016 at 3:56 PM, Marko Rauhamaa <marko@pacujo.net> wrote:
> Ray Cote <rgacote@appropriatesolutions.com>:
>
>> I’m trying to perform an synchronous task while using asyncio.
>
> You seem to want to do something you shouldn't be doing. Asyncio does
> not tolerate synchronous/blocking calls. You will need to move those in
> separate threads or processes if you can't turn them into asynchronous
> tasks.

That's exactly what run_in_executor does.

-- 
Zach

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web