Author aeros
Recipients aeros, asvetlov, primal, yselivanov
Date 2019-11-17.06:33:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1573972409.97.0.124050280442.issue32309@roundup.psfhosted.org>
In-reply-to
Content
So, I just had an interesting idea... what if ThreadPool.run() returned a Task instead of a coroutine object?

With the current version of asyncio.ThreadPool, if a user wants to create a Task, they would have to do something like this:

async with asyncio.ThreadPool() as pool:
    task = asyncio.create_task(pool.run(io_blocking_func, 10, kwarg1='test'))
    other_task = asyncio.create_task(pool.run(io_blocking_func, 12))
    if some_conditional:
        task.cancel()
    results = await asyncio.gather(task, other_task, return_exceptions=True)
    ...

To me, this looks like excessive boilerplate, particularly for a higher level API. Even for rather straightforward behavior, it requires nested function calls. If we were to return a task directly, this would be significantly cleaner:

async with asyncio.ThreadPool() as pool:
    task = pool.run(io_blocking_func, 10, kwarg1='test')
    other_task = pool.run(io_blocking_func, 12)
    if some_conditional:
        task.cancel()
    results = await asyncio.gather(task, other_task, return_exceptions=True)
    ...

Since asyncio.ThreadPool is intended to be a high-level API, I don't think it's an issue to return a Task from it's run() method. It would make it significantly easier and more convenient to work with from a user perspective.

Thoughts?
History
Date User Action Args
2019-11-17 06:33:30aerossetrecipients: + aeros, asvetlov, yselivanov, primal
2019-11-17 06:33:29aerossetmessageid: <1573972409.97.0.124050280442.issue32309@roundup.psfhosted.org>
2019-11-17 06:33:29aeroslinkissue32309 messages
2019-11-17 06:33:29aeroscreate