I liked the ability to turn a function into a thread without the unnecessary line to define a class. I know about _thread, however it appears that you are not supposed to use _thread. Is there a good-practice equivalent of thread.start_new_thread for python 3?
Asked
Active
Viewed 4.4k times
2 Answers
48
threading.Thread(target=some_callable_function).start()
or if you wish to pass arguments,
threading.Thread(target=some_callable_function,
args=(tuple, of, args),
kwargs={'dict': 'of', 'keyword': 'args'},
).start()

Amber
- 507,862
- 82
- 626
- 550
-
I am using method 2 to pass one argument to a thread, and am getting the error: argument after * must be sequence, not it. Do you know what this means? – lemiant Jun 12 '11 at 00:17
-
11Make sure you're passing `(firstarg,)` not `(firstarg)` - remember that single-element tuples need the trailing comma to be interpreted as a tuple. – Amber Jun 12 '11 at 00:29
-
What would the callable method signature look like? If I use something like `callable(**kwargs):` I get `TypeError: callable() takes 0 positional arguments but 1 was given`, and if I use `callable(kwargs):` I get `TypeError: callable() got an unexpected keyword argument 'raw'`. – henrikstroem Feb 19 '15 at 10:37
-
1@henrikstroem `threading.Thread(target=callable,kwargs=kwargs)` – Smart Manoj May 23 '18 at 02:04
5
Unfortunately there is not a direct equivalent, because Python 3 is meant to be more portable than Python 2 and the _thread
interface is seen as too low-level for this purpose.
In Python 3 the best practice is usually to use threading.Thread(target=f...)
. This uses different semantics, but is preferred because the interface is easier to port to other Python implementations.

Jeremy
- 1
- 85
- 340
- 366