Skip to content

Commit 6f10271

Browse files
committed
gh-155334: Add start parameter to threading.Thread
Use start=True in the stdlib.
1 parent 7c653e2 commit 6f10271

14 files changed

Lines changed: 50 additions & 30 deletions

File tree

Doc/library/threading.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ since it is impossible to detect the termination of alien threads.
510510

511511

512512
.. class:: Thread(group=None, target=None, name=None, args=(), kwargs={}, *, \
513-
daemon=None, context=None)
513+
daemon=None, context=None, start=False)
514514

515515
This constructor should always be called with keyword arguments. Arguments
516516
are:
@@ -545,6 +545,8 @@ since it is impossible to detect the termination of alien threads.
545545
current context, pass the value from :func:`~contextvars.copy_context`. The
546546
flag defaults true on free-threaded builds and false otherwise.
547547

548+
If *start* is true, start immediately the thread after initialization.
549+
548550
If the subclass overrides the constructor, it must make sure to invoke the
549551
base class constructor (``Thread.__init__()``) before doing anything else to
550552
the thread.
@@ -558,6 +560,9 @@ since it is impossible to detect the termination of alien threads.
558560
.. versionchanged:: 3.14
559561
Added the *context* parameter.
560562

563+
.. versionchanged:: next
564+
Added the *start* parameter.
565+
561566
.. method:: start()
562567

563568
Start the thread's activity.

Doc/whatsnew/3.16.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,13 @@ symtable
461461
(Contributed by Serhiy Storchaka in :gh:`153844`.)
462462

463463

464+
threading
465+
---------
466+
467+
* Add *start* parameter to :class:`threading.Thread` to start immediately the
468+
thread after initialization
469+
(Contributed by Victor Stinner in :gh:`155334`.)
470+
464471
tkinter
465472
-------
466473

Lib/concurrent/futures/thread.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ def weakref_cb(_, q=self._work_queue):
233233
t = threading.Thread(name=thread_name, target=_worker,
234234
args=(weakref.ref(self, weakref_cb),
235235
self._create_worker_context(),
236-
self._work_queue))
237-
t.start()
236+
self._work_queue),
237+
start=True)
238238
self._threads.add(t)
239239
_threads_queues[t] = self._work_queue
240240

Lib/concurrent/interpreters/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,6 @@ def call_in_thread(self, callable, /, *args, **kwargs):
242242
243243
The return value and any raised exception are discarded.
244244
"""
245-
t = threading.Thread(target=self._call, args=(callable, args, kwargs))
246-
t.start()
247-
return t
245+
return threading.Thread(target=self._call,
246+
args=(callable, args, kwargs),
247+
start=True)

Lib/idlelib/pyshell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def __request_interrupt(self):
543543
self.rpcclt.remotecall("exec", "interrupt_the_server", (), {})
544544

545545
def interrupt_subprocess(self):
546-
threading.Thread(target=self.__request_interrupt).start()
546+
threading.Thread(target=self.__request_interrupt, start=True)
547547

548548
def kill_subprocess(self):
549549
if self._afterid is not None:

Lib/idlelib/run.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def main(del_exitfunc=False):
158158
name='SockThread',
159159
args=((LOCALHOST, port),),
160160
daemon=True,
161-
).start()
161+
start=True)
162162

163163
while True:
164164
try:

Lib/multiprocessing/managers.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ def serve_forever(self):
172172
self.stop_event = threading.Event()
173173
process.current_process()._manager_server = self
174174
try:
175-
accepter = threading.Thread(target=self.accepter)
176-
accepter.daemon = True
177-
accepter.start()
175+
accepter = threading.Thread(target=self.accepter,
176+
daemon=True, start=True)
178177
try:
179178
while not self.stop_event.is_set():
180179
self.stop_event.wait(1)
@@ -193,9 +192,8 @@ def accepter(self):
193192
c = self.listener.accept()
194193
except OSError:
195194
continue
196-
t = threading.Thread(target=self.handle_request, args=(c,))
197-
t.daemon = True
198-
t.start()
195+
threading.Thread(target=self.handle_request, args=(c,),
196+
daemon=True, start=True)
199197

200198
def _handle_request(self, c):
201199
request = None

Lib/multiprocessing/resource_sharer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@ def _start(self):
125125
util.debug('starting listener and thread for sending handles')
126126
self._listener = Listener(authkey=process.current_process().authkey, backlog=128)
127127
self._address = self._listener.address
128-
t = threading.Thread(target=self._serve)
129-
t.daemon = True
130-
t.start()
128+
t = threading.Thread(target=self._serve, daemon=True, start=True)
131129
self._thread = t
132130

133131
def _serve(self):

Lib/profiling/sampling/_child_monitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ def __enter__(self):
8888
target=self._monitor_loop,
8989
daemon=True,
9090
name=f"child-monitor-{self.parent_pid}",
91+
start=True,
9192
)
92-
self._monitor_thread.start()
9393
return self
9494

9595
def __exit__(self, exc_type, exc_val, exc_tb):

Lib/profiling/sampling/gecko_collector.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,7 @@ def spin():
749749
sys.stderr.write('\r' + ' ' * (len(message) + 3) + '\r')
750750
sys.stderr.flush()
751751

752-
spinner_thread = threading.Thread(target=spin, daemon=True)
753-
spinner_thread.start()
752+
spinner_thread = threading.Thread(target=spin, daemon=True, start=True)
754753

755754
temp_path = None
756755
replaced = False

0 commit comments

Comments
 (0)