python / python/cpython

Add `add_running_callback()` and `remove_running_callback()` methods to `concurrent.futures.Future` and `asyncio.Future`

未关闭
#101,344 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-feature
主要语言
Python
星标
77.2k
派生
36k
PR 合并指标
PR 指标待抓取

描述

Feature or enhancement

Add add_running_callback() and remove_running_callback() methods to concurrent.futures.Future and asyncio.Future

Pitch

The concurrent.futures.Future and asyncio.Future module in Python provide a convenient way to run parallel and asynchronous tasks. However, they currently lacks the ability to add and remove callbacks that are executed when a Future is running. This proposal aims to add two new methods to the Future class, add_running_callback() and remove_running_callback(), which will allow developers to add and remove callbacks that are executed when a Future is running. This will provide more flexibility and control over the execution of parallel and asynchronous tasks.

Example usage:

Here is an example of how the new methods will be used:

import concurrent.futures
import time

def some_long_running_task():
    print('Running a long running task')
    time.sleep(10)
    return 42

def running_callback_1(future):
    print(f'{future} is running')

def running_callback_2(future):
    print(f'{future} is still running')

with concurrent.futures.ThreadPoolExecutor() as executor:
    future = executor.submit(some_long_running_task)
    future.add_running_callback(running_callback_1)
    future.add_running_callback(running_callback_2)
    # do something else here like ping a server
    future.remove_running_callback(running_callback_1)
    # do something else here like fetch some data
    future.remove_running_callback(running_callback_1)
    # do something else here 

In the above example, running_callback_1 and running_callback_2 are added and then removed from the list of running callbacks for the Future. Here is how to achieve the same but in blocking manner:

import concurrent.futures
import time

def some_long_running_task():
    print('Running a long running task')
    time.sleep(10)
    return 42

def running_callback(future):
    print(f'{future} is running')
    print(f'ping or fetch something while {future} is running')
    print(f'check something else while {future} is running')

def done_callback(future):
    print(f'{future} is done and returned {future.result()}')

def while_running(future):
    while not future.done():
        print('Running a long running task')
        time.sleep(1)

def main():
    with concurrent.futures.ThreadPoolExecutor() as executor:
        future = executor.submit(some_long_running_task)
        future.add_done_callback(done_callback)
        while_running(future)

if __name__ == '__main__':
    main()

Previous discussion

This feature has not been previously discussed.

Design and Implementation

The proposed add_running_callback() and remove_running_callback() methods will be added to the Future class in both the concurrent.futures.Future and asyncio.Future. The add_running_callback() method will take a single argument, a callable object, and will add it to a list of callbacks to be executed when the Future is running. The remove_running_callback() method will also take a single argument, a callable object, and will remove it from the list of callbacks.

The add_running_callback() and remove_running_callback() methods will be implemented by adding a new attribute, _running_callbacks, to the Future class. This attribute will be a list of callbacks that are executed when the Future is running. The add_running_callback() method will simply append the passed callback to the list, and the remove_running_callback() method will remove it.

In addition, the Future.init method will be updated to initialize the _running_callbacks attribute as an empty list. And the Future._invoke_callbacks method will be updated to invoke the callbacks in the _running_callbacks list, in addition to the done callbacks, when the Future's state changes.

Backwards compatibility

The proposed changes to the Future class will not affect any existing code that uses both the concurrent.futures.Future and asyncio.Future. The new methods, add_running_callback() and remove_running_callback(), are optional and will not be used by any code that does not specifically call them.

Conclusion

Adding the add_running_callback() and remove_running_callback() methods to the Future class in both the concurrent.futures.Future and asyncio.Future will provide greater flexibility and control over the execution of parallel and asynchronous tasks. This proposal is backwards-compatible and provides a simple and straightforward solution to a common problem faced by developers using both the concurrent.futures.Future and asyncio.Future.

P.S. This would be my first contribution and I have already had quick look at Python Developer’s Guide and PEP Purpose and Guidelines, and I believe this kind of proposals falls under enhancements effort but if I missed something or this is not the right place for this proposal, please kindly let me know.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先阅读 concurrent.futures.Future 和 asyncio.Future 的实现及其现有的回调行为。定义正在运行的回调语义,包括何时调用回调以及移除的行为,然后更新两个 Future 类,并验证新方法和回调调用路径。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
backend
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。