Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improve concurrent.interpreters documentation #142041

Open
@FabriceSalvaire

Description

@FabriceSalvaire

Documentation

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

I tried this, but I don't understand

  • how to usequeue
  • prepare_main purpose
  • exec parameters likededent
  • I got funny crashes
####################################################################################################fromconcurrentimportinterpretersasCIfrompprintimportpprintfromtextwrapimportdedentimportqueue####################################################################################################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()deftask_runner(index):print("Task runner",index)print(f"  Current is{current_interpreter}")globals_=globals()# pprint(globals_)print("  Globals:")forkey,valueinglobals().items():ifkey=='__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:ifcurrent_interpreterismain_interpreter:# queue = CI.create_queue()interpreters= []NUMBER_OF_INTERPRETERS=4foriinrange(NUMBER_OF_INTERPRETERS):_=CI.create()# Bind objects in the interpreter’s __main__ module_.prepare_main(ns=None,foo=123)interpreters.append(_)for_inCI.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= []foriinrange(NUMBER_OF_INTERPRETERS):thread=interpreters[i].call_in_thread(task_runner,i)print(f"Thread #{i}{thread}")threads.append(thread)NUMBER_OF_TASKS=10foriinrange(NUMBER_OF_TASKS):queue.put(i)forthreadinthreads:thread.join()# print()# print(queue)# Block main#! print(queue.get())# try:# except queue.ShutDown:#     passelse:pass

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Todo

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp