python / python/cpython

Improve concurrent.interpreters documentation

未关闭
#142,041 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

docs topic-subinterpreters
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Documentation

We need a better documentation for https://docs.python.org/3/library/concurrent.interpreters.html

I tried this, but I don't understand

  • how to use queue
  • prepare_main purpose
  • exec parameters like dedent
  • I got funny crashes
####################################################################################################

from concurrent import interpreters as CI
from pprint import pprint
from textwrap import dedent
import queue

####################################################################################################

main_interpreter = CI.get_main()
print(f"Main is {main_interpreter}")

current_interpreter = CI.get_current()
print(f"Current is {current_interpreter}")

queue = CI.create_queue()

def task_runner(index):
    print("Task runner", index)
    print(f"  Current is {current_interpreter}")

    globals_ = globals()
    # pprint(globals_)
    print("  Globals:")
    for key, value in globals().items():
        if key == '__builtins__':
            print(f"    __builtins__: ...")
        else:
            print(f"    {key}: {value}")

    # Crash
    #   Main is Interpreter(0)
    #   Current is Interpreter(1)
    #   concurrent.interpreters.ExecutionFailed: NameError: name 'foo' is not defined
    #! print(foo)

    # Block
    #! print(f"  #{index} from queue {queue.get()}")

    return 'ok'

# if True:
if current_interpreter is main_interpreter:
    # queue = CI.create_queue()

    interpreters = []
    NUMBER_OF_INTERPRETERS = 4
    for i in range(NUMBER_OF_INTERPRETERS):
        _ = CI.create()
        # Bind objects in the interpreter’s __main__ module
        _.prepare_main(ns=None, foo=123)
        interpreters.append(_)

    for _ in CI.list_all():
        print(f"Interpreter id {_.id} running {_.is_running()} whence '{_.whence}'")

    print("Call...")

    print()
    # exec(code, /, dedent=True)
    interpreters[0].exec(dedent('''
    print(f"Exec hello {foo}")
    '''))

    print()
    rc = interpreters[0].call(task_runner, 0)
    print(rc)

    print()
    threads = []
    for i in range(NUMBER_OF_INTERPRETERS):
        thread = interpreters[i].call_in_thread(task_runner, i)
        print(f"Thread #{i} {thread}")
        threads.append(thread)

    NUMBER_OF_TASKS = 10
    for i in range(NUMBER_OF_TASKS):
        queue.put(i)

    for thread in threads:
        thread.join()

    # print()
    # print(queue)

    # Block main
    #! print(queue.get())
    # try:
    # except queue.ShutDown:
    #     pass
else:
    pass

Linked PRs
  • gh-145657

贡献指南

打开贡献指南

从这里开始

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

调研方向

从 docs.python.org/3/library/concurrent.interpreters.html 的 concurrent.interpreters 文档开始,并复现提供的示例。阐明队列的使用、prepare_main、exec 的参数(例如 dedent)以及报告的崩溃问题;当文档通过一个可运行且易于理解的示例解释这些情况时,即可视为完成。

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

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

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