Search code examples
pythonmultithreadingpython-multithreading

Thread vs. Threading


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


Solution

  • 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.

    Note: as mentioned in Jeril's alternate answer, when considering the use of threads in Python for execution of background tasks, also take a look at concurrent.futures.ThreadPoolExecutor as an even higher level API than threading: https://docs.python.org/3/library/concurrent.futures.html#concurrent.futures.ThreadPoolExecutor