Skip to content

Rad-hi/Python_multi_tasking

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Multi-tasking with Python

This repo contains examples on how to use the threading and multiprocessing builtin modules in Python.

Normal Python code runs in a single-core, single-thread fashion, which might be okay in many cituations, but in some other, it just doesn't make sense to run all code sequentially.

Here comes multi-tasking. We say that someone is multi-tasking when they're doing multiple tasks at the same time. So we can say that multi-taskingin Python is when we make our code do multiple tasks at the same time.

Multi-tasking could be achieved in two seemingly similar but different manners:

  • Multi-processing: We often hear the words "Dual-core", or "Quad-core", ..., whenever we're talking about a CPU. The "Quad-core" for example means that the CPU has 4 physical CPUs inside of it all running at the same time sharing tasks. Multi-processing is the process of optimizing tasks to run simultaneously each on a seperate core. This gives us an advantage in terms of speed(reducing runtime). Beware though of codes that are sharing ressources since the data-transfer between cores should be managed by the programmer(You!).

Notice the core PIDs changing and the gain in run-time.

  • Multi-threading: Similarly to Multi-processing, we often hear the words "Quad-core, 8 Threads" when talking about CPUs. Threads are virtual Cores(CPUs) that live on a physical Core(CPU). They're used also to speed up processes but in Python, multi-threading doesn't exactly run tasks simultaneously, instead it sequentially jumps between tasks without waiting for one to finish and end up saving idle times. Since multi-threading runs on the same core, the problems of data-transfer between cores that arises in the multiprocessing case isn't present.

Notice the same core PID for all examples with the gain in run-time.


References:

Multi-processing VS Multi-threading

Multiprocessing tutorial with RAY(parallel/distributed computing with Python)

About

Examples on multi-processing and multi-threading in Python.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages