Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

threading.Thread.start should return self #93293

Open
tewalds opened this issue May 27, 2022 · 3 comments
Open

threading.Thread.start should return self #93293

tewalds opened this issue May 27, 2022 · 3 comments
Labels
docs Documentation in the Doc dir stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@tewalds
Copy link

tewalds commented May 27, 2022

I often write code like:

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

for t in threads:
  t.join()

and would really prefer:

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

Is there a reason why start doesn't return self here: https://github.com/python/cpython/blob/3.10/Lib/threading.py#L940 ?

An alternative would be to add a start: bool = False param to the Thread constructor such that you can construct it with start=True. I'm not sure why you'd ever want to construct a Thread but not start it, so I wish that could default to true, but clearly it can't due to backwards compatibility.

@AA-Turner AA-Turner added type-feature A feature request or enhancement stdlib Python modules in the Lib dir 3.12 bugs and security fixes labels May 27, 2022
@rhettinger
Copy link
Contributor

The norm in Python is for mutating methods to return None.

@serhiy-storchaka
Copy link
Member

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

It is an antipattern. If some of start() fails you will lose references to all previously started threads, and you cannot know what threads have been started. It is bad for resource management.

@tewalds
Copy link
Author

tewalds commented May 30, 2022

The documentation doesn't give any reason to believe it can fail in normal usage. The only documented way to fail is if you call start more than once, which clearly doesn't happen in a list comprehension as above.

This version:

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

would also lose the references to all the started threads in threads if start raised an exception. Sure, it's easier to add a try/except, but at the moment it's way more verbose and no safer, with no indication what could fail or how to recover.

Assuming it can fail, is additional documentation in order then to specify how it can fail and what to expect when it does so it could possibly be dealt with?

@erlend-aasland erlend-aasland added docs Documentation in the Doc dir and removed 3.12 bugs and security fixes labels Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
Status: No status
Development

No branches or pull requests

5 participants