threads and concurrency when multiple processes or threads are usedthe host operating system is responsible for scheduling their work this is done by giving each process (or threada small time slice and rapidly cycling between all of the active tasks--giving each portion of the available cpu cycles for exampleif your system had active processes runningthe operating system would allocate approximately / th of its cpu time to each process and cycle between processes in rapid succession on systems with more than one cpu corethe operating system can schedule processes so that each cpu is kept busyexecuting processes in parallel writing programs that take advantage of concurrent execution is something that is intrinsically complicated major source of complexity concerns synchronization and access to shared data in particularattempts to update data structure by multiple tasks at approximately the same time can lead to corrupted and inconsistent program state ( problem formally known as race conditionto fix these problemsconcurrent programs must identify critical sections of code and protect them using mutual-exclusion locks and other similar synchronization primitives for exampleif different threads were trying to write data to the same file at the same timeyou might use mutual exclusion lock to synchronize their operation so that once one of the threads starts writingthe other threads have to wait until it has finished before they are allowed to start writing the code for this scenario typically looks like thiswrite_lock lock(critical section where writing occurs write_lock acquire( write("here' some data \ " write("here' more data \ "write_lock release(there' joke attributed to jason whittington that goes as like this"why did the multithreaded chicken cross the roadto to other side get thethis joke typifies the kinds of problems that arise with task synchronization and concurrent programming if you're scratching your head saying" don' get it,then it might be wise to do bit more reading before diving into the rest of this concurrent programming and python python supports both message passing and thread-based concurrent programming on most systems although most programmers tend to be familiar with the thread interfacepython threads are actually rather restricted although minimally thread-safethe python interpreter uses an internal global interpreter lock (the gilthat only allows single python thread to execute at any given moment this restricts python programs to run on single processor regardless of how many cpu cores might be available on the system although the gil is often heated source of debate in the python communityit is unlikely to be removed at any time in the foreseeable future the presence of the gil has direct impact on how many python programmers address concurrent programming problems if an application is mostly / boundit is generally fine to use threads because extra processors aren' going to do much to help program that spends most of its time waiting for events for applications that involve heavy amounts of cpu processingusing threads to subdivide work doesn' provide any benefit and will make the program run slower (often much slower than you would guessfor thisyou'll want to rely on subprocesses and message passing lib fl ff