threads and concurrency his describes library modules and programming strategies for writing concurrent programs in python topics include threadsmessage passingmultiprocessingand coroutines before covering specific library modulessome basic concepts are first described basic concepts running program is called process each process has its own system statewhich includes memorylists of open filesa program counter that keeps track of the instruction being executedand call stack used to hold the local variables of functions normallya process executes statements one after the other in single sequence of control flowwhich is sometimes called the main thread of the process at any given timethe program is only doing one thing program can create new processes using library functions such as those found in the os or subprocess modules ( os fork()subprocess popen()etc howeverthese processesknown as subprocessesrun as completely independent entities--each with their own private system state and main thread of execution because subprocess is independentit executes concurrently with the original process that isthe process that created the subprocess can go on to work on other things while the subprocess carries out its own work behind the scenes although processes are isolatedthey can communicate with each other--something known as interprocess communication (ipcone of the most common forms of ipc is based on message passing message is simply buffer of raw bytes primitive operations such as send(and recv(are then used to transmit or receive messages through an / channel such as pipe or network socket another somewhat less common ipc mechanism relies upon memory-mapped regions (see the mmap modulewith memory mappingprocesses can create shared regions of memory modifications to these regions are then visible in all processes that happen to be viewing them multiple processes can be used by an application if it wants to work on multiple tasks at the same time--with each process responsible for part of the processing howeveranother approach for subdividing work into tasks is to use threads thread is similar to process in that it has its own control flow and execution stack howevera thread runs inside the process that created itsharing all of the data and system resources threads are useful when an application wants to perform tasks concurrentlybut there is potentially large amount of system state that needs to be shared by the tasks lib fl ff