Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happened to thread.start_new_thread in python 3

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?

like image 736
lemiant Avatar asked Jun 12 '11 00:06

lemiant


People also ask

Does Python 3 support multithreading?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading.

How do I run a thread in Python 3?

Creating Thread Using Threading ModuleDefine a new subclass of the Thread class. Override the __init__(self [,args]) method to add additional arguments. Then, override the run(self [,args]) method to implement what the thread should do when started.

Is Python really multi threaded?

Python is NOT a single-threaded language. Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.

How do I enable threads in Python?

You need to assign the thread object to a variable and then start it using that varaible: thread1=threading. Thread(target=f) followed by thread1. start() . Then you can do thread1.


2 Answers

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() 
like image 142
Amber Avatar answered Sep 16 '22 14:09

Amber


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.

like image 32
Jeremy Avatar answered Sep 17 '22 14:09

Jeremy