coroutine

Proper type annotation of Python functions with yield

Proper type annotation of Python functions with yield Question: After reading Eli Bendersky’s article on implementing state machines via Python coroutines I wanted to… see his example run under Python3 and also add the appropriate type annotations for the generators I succeeded in doing the first part (but without using async defs or yield froms, …

Total answers: 3

asyncio.ensure_future vs. BaseEventLoop.create_task vs. simple coroutine?

asyncio.ensure_future vs. BaseEventLoop.create_task vs. simple coroutine? Question: I’ve seen several basic Python 3.5 tutorials on asyncio doing the same operation in various flavours. In this code: import asyncio async def doit(i): print(“Start %d” % i) await asyncio.sleep(3) print(“End %d” % i) return i if __name__ == ‘__main__’: loop = asyncio.get_event_loop() #futures = [asyncio.ensure_future(doit(i), loop=loop) for …

Total answers: 4

Test if function or method is normal or asynchronous

Test if function or method is normal or asynchronous Question: How can I find out if a function or method is a normal function or an async function? I would like my code to automatically support normal or async callbacks and need a way to test what type of function is passed. async def exampleAsyncCb(): …

Total answers: 8

Python native coroutines and send()

Python native coroutines and send() Question: Generator based coroutines have a send() method which allow bidirectional communication between the caller and the callee and resumes a yielded generator coroutine from the caller. This is the functionality that turns generators into coroutines. While the new native async/await coroutines provide superior support for async I/O, I do …

Total answers: 3

What's the difference between loop.create_task, asyncio.async/ensure_future and Task?

What's the difference between loop.create_task, asyncio.async/ensure_future and Task? Question: I’m a little bit confused by some asyncio functions. I see there is BaseEventLoop.create_task(coro) function to schedule a co-routine. The documentation for create_task says its a new function and for compatibility we should use asyncio.async(coro) which by referring to docs again I see is an alias …

Total answers: 2

How to use async/await in Python 3.5?

How to use async/await in Python 3.5? Question: #!/usr/bin/env python3 # -*- coding: utf-8 -*- import time async def foo(): await time.sleep(1) foo() I couldn’t make this dead simple example to run: RuntimeWarning: coroutine ‘foo’ was never awaited foo() Asked By: smilingpoplar || Source Answers: Running coroutines requires an event loop. Use the asyncio() library …

Total answers: 2

Python – how to run multiple coroutines concurrently using asyncio?

How to run multiple coroutines concurrently using asyncio? Question: I’m using the websockets library to create a websocket server in Python 3.4. Here’s a simple echo server: import asyncio import websockets @asyncio.coroutine def connection_handler(websocket, path): while True: msg = yield from websocket.recv() if msg is None: # connection lost break yield from websocket.send(msg) start_server = …

Total answers: 4

PEP 0492 – Python 3.5 async keyword

PEP 0492 – Python 3.5 async keyword Question: PEP 0492 adds the async keyword to Python 3.5. How does Python benefit from the use of this operator? The example that is given for a coroutine is async def read_data(db): data = await db.fetch(‘SELECT …’) According to the docs this achieves suspend[ing] execution of read_data coroutine …

Total answers: 1

Can Gevent be used in combination with real threads in CPython?

Can Gevent be used in combination with real threads in CPython? Question: just wondering whether gevent coro-thread can work with real threading? perhaps I can write a program with multiple threads and each thread has some coroutines in them? edited both gevent.Threading and CPython threading have their down side. coroutines cannot utilize multiple CPUs, while …

Total answers: 1