How do I write a sequence of promises in Python?

Here’s a similar program using asyncio and the async/await syntax: import asyncio import random async def alpha(x): await asyncio.sleep(0.2) return x + 1 async def bravo(x): await asyncio.sleep(0.2) return random.randint(0, 1000) + x async def charlie(x): if x % 2 == 0: return x raise ValueError(x, ‘is odd’) async def run(): try: number = await … Read more

Equivalent of asyncio.Queues with worker “threads”

Yes, exactly. Tasks are your friends: import asyncio, random q = asyncio.Queue() @asyncio.coroutine def produce(): while True: yield from q.put(random.random()) yield from asyncio.sleep(0.5 + random.random()) @asyncio.coroutine def consume(): while True: value = yield from q.get() print(“Consumed”, value) loop = asyncio.get_event_loop() loop.create_task(produce()) loop.create_task(consume()) loop.run_forever() asyncio.ensure_future can be used for task creation also. And please keep in … Read more

ObjectNotExecutableError when executing any SQL query using AsyncEngine

As the exception message suggests, the str ‘SELECT id, name FROM item LIMIT 50;’ is not an executable object. To make it executable, wrap it with sqlalchemy.text. from sqlalchemy import text async with self.async_engine.connect() as con: query = “SELECT id, name FROM item LIMIT 50;” result = await con.execute(text(query)) async.connection.execute requires that its statement argument … Read more

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

As you’ve noticed, they all do the same thing. asyncio.async had to be replaced with asyncio.ensure_future because in Python >= 3.5, async has been made a keyword[1]. create_task‘s raison d’etre[2]: Third-party event loops can use their own subclass of Task for interoperability. In this case, the result type is a subclass of Task. And this … Read more

Sharing python objects across multiple workers

It is not possible to share a python object between different processes straightforwardly. The facilities included in the multiprocessing module (like managers or shared memory) are not suitable for sharing resources between workers, since they require a master process creating the resources and do not have the durability property. Also server processes can be run … Read more

Differences between Futures in Python3 and Promises in ES6

In both Python and ES6, await/async are based on generators. Is it a correct to think Futures are the same as Promises? Not Future, but Python’s Task is roughly equivalent to Javascript’s Promise. See more details below. I have seen the terms Task, Future and Coroutine used in the asyncio documentation. What are the differences … Read more

Python type hinting for async function as function argument

You are looking for: FuncType = Callable[[Any, Any], Awaitable[Any]] def consumer(function_: FuncType = None): pass # TODO: do stuff Why is the type structured like that? If you declare a function async, what you actually do is wrap it in a new function with the given parameters, which returns a Coroutine. Since this might be … Read more

Why can’t I ‘yield from’ inside an async function?

According to PEP 525, which introduces asyncronous generators in Python 3.6: Asynchronous yield from While it is theoretically possible to implement yield from support for asynchronous generators, it would require a serious redesign of the generators implementation. yield from is also less critical for asynchronous generators, since there is no need provide a mechanism of … Read more