Execute coroutine from `call_soon` callback function

Michael

I have following situation:

  • Some internal class (which I have no control of) is executing my callback function using call_soon.
  • Within my callback I would like to call another courotune, but end up with "frozen" callback.

I will use modified Hello World with call_soon() to demonstrate this:

import asyncio

def hello_world(loop):
    print('Hello')
    # Call some coroutine.
    yield from asyncio.sleep(5, loop=loop)
    print('World')
    loop.stop()

loop = asyncio.get_event_loop()

# Schedule a call to hello_world()
loop.call_soon(hello_world, loop)

# Blocking call interrupted by loop.stop()
loop.run_forever()
loop.close()

When I run this, nothing is being printed and the program never ends.

Ctrl+C

Traceback (most recent call last):
  File "../soon.py", line 15, in <module>
    loop.run_forever()
  File "/usr/lib/python3.4/asyncio/base_events.py", line 276, in run_forever
    self._run_once()
  File "/usr/lib/python3.4/asyncio/base_events.py", line 1136, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.4/selectors.py", line 432, in select
    fd_event_list = self._epoll.poll(timeout, max_ev)
KeyboardInterrupt

What is actually going on and why?

Any correct way to do this?

Vincent

The example you mentioned demonstrate how to schedule a callback.

If you use the yield from syntax, the function is actually a coroutine and it has to be decorated accordingly:

@asyncio.coroutine
def hello_world(loop):
    print('Hello')
    yield from asyncio.sleep(5, loop=loop)
    print('World')
    loop.stop()

Then you can schedule the coroutine as a task using ensure_future:

loop = asyncio.get_event_loop()
coro = hello_world(loop)
asyncio.ensure_future(coro)
loop.run_forever()
loop.close()

Or equivalently, using run_until_complete:

loop = asyncio.get_event_loop()
coro = hello_world(loop)
loop.run_until_complete(coro)

In two weeks, python 3.5 will officially be released and you'll be able to use the new async/await syntax:

async def hello_world(loop):
    print('Hello')
    await asyncio.sleep(5, loop=loop)
    print('World')

EDIT: It is a bit ugly, but nothing prevents you from creating a callback that schedules your coroutine:

loop = asyncio.get_event_loop()
coro = hello_world(loop)
callback = lambda: asyncio.ensure_future(coro)
loop.call_soon(callback)
loop.run_forever()
loop.close()

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to call Kotlin coroutine in composable function callbacks?

分類Dev

Execute function from ollydbg?

分類Dev

how to use Coroutine in kotlin to call a function every second

分類Dev

how create a coroutine inside a Controller method in order to call a suspend function

分類Dev

Call to a member function execute() on boolean in

分類Dev

Return value from a function callback in a for loop

分類Dev

Setting variable to a return from a callback function

分類Dev

No result from callback function in Node.js

分類Dev

Pass NodeJS callback from exported function to view

分類Dev

How can I Execute/Call a user-space defined function from Linux kernel space module?

分類Dev

Trying to execute a function from html in Angular 8

分類Dev

Difference between using call_soon() and ensure_future()

分類Dev

Call object property from within a callback

分類Dev

Have code execute after call to async function

分類Dev

Error thrown from a mongoose-promise callback function is not caught

分類Dev

Emitting a Node.js Event from a C++ callback function

分類Dev

Python Dash: Update Layout from Outside of Callback-Function

分類Dev

SwiftUI MVVM how to set callback from ViewModel function to View

分類Dev

Wait for return value from function or callback in node js

分類Dev

PhoneGap - return custom plugin callback value from method other than execute()

分類Dev

Execute non-function code before a function call

分類Dev

Vue js execute a child function from a parent and get data

分類Dev

Using TwilioClient to execute Studio Flow from a Runtime Function

分類Dev

How to rate limit a coroutine and re-call the coroutine after the limit?

分類Dev

Success callback function is not called

分類Dev

Callback function syntax in Swift

分類Dev

passing multiple callback in a function

分類Dev

Callback: "Object is not a function"

分類Dev

Using this in the chunk callback function

Related 関連記事

  1. 1

    How to call Kotlin coroutine in composable function callbacks?

  2. 2

    Execute function from ollydbg?

  3. 3

    how to use Coroutine in kotlin to call a function every second

  4. 4

    how create a coroutine inside a Controller method in order to call a suspend function

  5. 5

    Call to a member function execute() on boolean in

  6. 6

    Return value from a function callback in a for loop

  7. 7

    Setting variable to a return from a callback function

  8. 8

    No result from callback function in Node.js

  9. 9

    Pass NodeJS callback from exported function to view

  10. 10

    How can I Execute/Call a user-space defined function from Linux kernel space module?

  11. 11

    Trying to execute a function from html in Angular 8

  12. 12

    Difference between using call_soon() and ensure_future()

  13. 13

    Call object property from within a callback

  14. 14

    Have code execute after call to async function

  15. 15

    Error thrown from a mongoose-promise callback function is not caught

  16. 16

    Emitting a Node.js Event from a C++ callback function

  17. 17

    Python Dash: Update Layout from Outside of Callback-Function

  18. 18

    SwiftUI MVVM how to set callback from ViewModel function to View

  19. 19

    Wait for return value from function or callback in node js

  20. 20

    PhoneGap - return custom plugin callback value from method other than execute()

  21. 21

    Execute non-function code before a function call

  22. 22

    Vue js execute a child function from a parent and get data

  23. 23

    Using TwilioClient to execute Studio Flow from a Runtime Function

  24. 24

    How to rate limit a coroutine and re-call the coroutine after the limit?

  25. 25

    Success callback function is not called

  26. 26

    Callback function syntax in Swift

  27. 27

    passing multiple callback in a function

  28. 28

    Callback: "Object is not a function"

  29. 29

    Using this in the chunk callback function

ホットタグ

アーカイブ