Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102173 > unrolled thread
| Started by | "Frank Millman" <frank@chagford.com> |
|---|---|
| First post | 2016-01-28 08:18 +0200 |
| Last post | 2016-01-28 08:18 +0200 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Question about asyncio and blocking operations "Frank Millman" <frank@chagford.com> - 2016-01-28 08:18 +0200
| From | "Frank Millman" <frank@chagford.com> |
|---|---|
| Date | 2016-01-28 08:18 +0200 |
| Subject | Re: Question about asyncio and blocking operations |
| Message-ID | <mailman.43.1453961949.2338.python-list@python.org> |
"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 top | Article view | comp.lang.python
csiph-web