New submission from Antoine Pietri <antoine.piet...@gmail.com>:

In 3.7 a lot of high level functions have been added to the asyncio API, but 
nothing to start blocking functions as non-blocking threads. You still have to 
call get_event_loop() then await loop.run_in_executor(None, callable). I think 
this pattern is *very* common and could use a higher level interface.

Would an API like this make sense?

async def create_thread(callable, *args, *, kwargs=None, loop=None, 
timeout=None)

Then it could just be used like this:

await asyncio.create_thread(my_blocking_read, 4096, timeout=10)

This API could wrap the run_in_executor in an asyncio.Task, that way
you wouldn't have to await it to start the thread. There's evidence
that this has confused people in the past:

https://stackoverflow.com/questions/54263558/is-asyncio-run-in-executor-specified-ambiguously

----------
components: asyncio
messages: 353456
nosy: antoine.pietri, asvetlov, yselivanov
priority: normal
severity: normal
status: open
title: High level API for loop.run_in_executor(None, ...)?
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38306>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to