Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Doc/library/threading.rst
Comment thread
rqndom marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,33 @@ This module defines the following functions:
.. versionadded:: 3.4


.. function:: run(func, /, *args, **kwargs)
run_daemon(func, /, *args, **kwargs)
Comment thread
rqndom marked this conversation as resolved.

Run ``func(*args, **kwargs)`` in a thread and return the corresponding
:class:`Thread` object. The thread is started automatically.

With :func:`run_daemon`, the thread is set as daemonic.

Example:

.. code-block:: python

import threading, urllib

def fetch(url, data=None):
response = urllib.request.urlopen(url, data)
# further processing...

t1 = threading.run(fetch, 'https://example.com/')
t2 = threading.run(fetch, 'https://example.com/post', data=payload)

t1.join()
t2.join()

.. versionadded:: next


.. function:: settrace(func)

.. index:: single: trace function
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,14 @@ symtable
(Contributed by Serhiy Storchaka in :gh:`153844`.)


threading
---------

* Add :func:`threading.run` and :func:`threading.run_daemon`
as a convenient way to start threads.
(Contributed by Romain Vavassori in :gh:`156131`.)


tkinter
-------

Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,21 @@ def run_in_bg():
self.assertEqual(err, b"")
self.assertEqual(out.strip(), b"Exiting...")

def test_run(self):
def func(x, y):
z.append(x + y)

z = []
thread = threading.run(func, 5, y=3)
thread.join()
self.assertEqual(z, [8])

z = []
thread = threading.run_daemon(func, 8, y=4)
thread.join()
self.assertEqual(z, [12])
self.assertEqual(thread.daemon, True)

class ThreadJoinOnShutdown(BaseTestCase):

def _run_and_join(self, script):
Expand Down
16 changes: 15 additions & 1 deletion Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
'setprofile', 'settrace', 'local', 'stack_size',
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile',
'serialize_iterator', 'synchronized_iterator', 'concurrent_tee',
'setprofile_all_threads','settrace_all_threads']
'setprofile_all_threads','settrace_all_threads',
'run', 'run_daemon']

# Rename some stuff so "from threading import *" is safe
_start_joinable_thread = _thread.start_joinable_thread
Expand Down Expand Up @@ -1664,6 +1665,19 @@ def enumerate():
with _active_limbo_lock:
return list(_active.values()) + list(_limbo.values())

def run(func, /, *args, **kwargs):
"""Return a running Thread object of func(*args, **kwargs)."""
thread = Thread(target=func, name=func.__name__, args=args, kwargs=kwargs)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe name is set to func.__name__ by default by the Thread constructor.

@rqndom rqndom Aug 21, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Thread constructor the name defaults to "Thread-123 (func_name)", I find this cleaner

thread.start()
return thread

def run_daemon(func, /, *args, **kwargs):
"""Return a running daemonic Thread object of func(*args, **kwargs)."""
thread = Thread(target=func, name=func.__name__, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread


_threading_atexits = []
_SHUTTING_DOWN = False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add :func:`threading.run` and :func:`threading.run_daemon` functions as
a convenient way to start threads.
Loading