Skip to content

threading.Thread: Add start=False parameter to start immediately the thread #155334

Description

@vstinner

It's common to create multiple threads, start them, and then join them. Example:

threads = [
    threading.Thread(target=run_a),
    threading.Thread(target=run_b),
]
for thread in threads:
    thread.start()
...
for thread in threads:
    thread.join()

I propose adding a start=False parameter to threading.Thread to start immediately the thread. The example would become:

threads = [
    threading.Thread(target=run_a, start=True),
    threading.Thread(target=run_b, start=True),
]
...
for thread in threads:
    thread.join()

The new parameter would solve the old issue gh-93293 which requests to return self in start(). The issue example:

threads = []
for i in range(10):
  thread = threading.Thread(target=fn, args=(i,))
  thread.start()
  threads.append(thread)

for t in threads:
  t.join()

would become:

threads = [threading.Thread(target=fn, args=(i,), start=True) for i in range(10)]

for t in threads:
  t.join()

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions