Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread vs. Threading

What's the difference between the threading and thread modules in Python?

like image 212
banx Avatar asked Apr 06 '11 15:04

banx


People also ask

What is thread and threading module 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.

What is the difference between threading and multiprocessing?

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.

Which is better threading or multiprocessing?

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.

Why we use threading in Python?

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.


2 Answers

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.

like image 171
ncoghlan Avatar answered Oct 05 '22 18:10

ncoghlan


threading is just a higher level module that interfaces thread.

See here for the threading docs:

http://docs.python.org/library/threading.html

like image 43
Mike Lewis Avatar answered Oct 05 '22 18:10

Mike Lewis