What's the difference between the threading
and thread
modules in Python?
The threading module exposes all the methods of the thread module and provides some additional methods − threading. activeCount() − Returns the number of thread objects that are active. threading. currentThread() − Returns the number of thread objects in the caller's thread control.
By formal definition, multithreading refers to the ability of a processor to execute multiple threads concurrently, where each thread runs a process. Whereas multiprocessing refers to the ability of a system to run multiple processors concurrently, where each processor can run one or more threads.
Also, processes require more resources than threads. Hence, it is always better to have multiprocessing as the second option for IO-bound tasks, with multithreading being the first.
Python threads are used in cases where the execution of a task involves some waiting. One example would be interaction with a service hosted on another computer, such as a webserver. Threading allows python to execute other code while waiting; this is easily simulated with the sleep function.
In Python 3, thread
has been renamed to _thread
. It is infrastructure code that is used to implement threading
, and normal Python code shouldn't be going anywhere near it.
_thread
exposes a fairly raw view of the underlying OS level processes. This is almost never what you want, hence the rename in Py3k to indicate that it is really just an implementation detail.
threading
adds some additional automatic accounting, as well as several convenience utilities, all of which makes it the preferred option for standard Python code.
threading
is just a higher level module that interfaces thread
.
See here for the threading
docs:
http://docs.python.org/library/threading.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With