Python | Multi-process cannot share global variables & multi-threading to achieve multi-tasking

Multiple processes cannot share global variables
import os, multiprocessing, threading

n = 100


def test():
global
n += 1
print('test==={} where the value of n is {}'.format(os.getpid(), hex(id(n))))


def demo():
global
n += 1
print('demo===={} the value of n is {}'.format(os.getpid(), hex(id(n))))


print(threading. current_thread(). name)
test() # 101
demo() # 102

# Two child threads in the same main process. Global variables of the same process can be shared between threads

# t1 = threading.Thread(target=test)
# t2 = threading.Thread(target=demo)
# t1. start()
# t2. start()

# if __name__ == '__main__':
# Different processes save a copy of global variables, and will not share global variables
# p1 = multiprocessing. Process(target=test)
# p2 = multiprocessing. Process(target=demo)
# p1. start() # 101
# p2. start() # 101
Example:

from multiprocessing import Process
import os

nums = [11, 22]

def work1():
"""The code to be executed by the child process"""
print("in process1 pid=%d ,nums=%s" % (os.getpid(), nums))
for i in range(3):
nums.append(i)
print("in process1 pid=%d ,nums=%s" % (os.getpid(), nums))

def work2():
"""The code to be executed by the child process"""
nums. pop()
print("in process2 pid=%d ,nums=%s" % (os.getpid(), nums))

if __name__ == '__main__':
p1 = Process(target=work1)
p1. start()
p1. join()

p2 = Process(target=work2)
p2. start()

print('in process0 pid={} ,nums={}'.format(os.getpid(),nums))
operation result:

in process1 pid=2707 ,nums=[11, 22]
in process1 pid=2707 ,nums=[11, 22, 0]
in process1 pid=2707 ,nums=[11, 22, 0, 1]
in process1 pid=2707 ,nums=[11, 22, 0, 1, 2]
in process0 pid=2706 ,nums=[11, 22]
in process2 pid=2708 ,nums=[11]

Multithreading for multitasking

In real life, there are many scenes where things are going on at the same time, such as dancing and singing are going on at the same time.

In the program, you can use code to simulate the singing and dancing functions:

from time import sleep

def sing():
for i in range(3):
print("Singing...%d"%i)
sleep(1)

def dance():
for i in range(3):
print("Dancing...%d"%i)
sleep(1)

if __name__ == '__main__':
sing() # sing
dance() # dance
Obviously the program just now did not fulfill the requirement of singing and dancing at the same time
If you want to achieve "singing and dancing" at the same time, then you need a new method called: multitasking

multitasking concept

What is "multitasking"? Simply put, the operating system can run multiple tasks at the same time. For example, you are surfing the Internet with a browser, listening to MP3, and catching up with homework with Word. This is multitasking, and at least three tasks are running at the same time. There are still many tasks running quietly in the background at the same time, but they are not displayed on the desktop.

Nowadays, multi-core CPUs are very popular, but even single-core CPUs of the past can perform multitasking. Since the CPU executes codes sequentially, how does a single-core CPU perform multitasking?

The answer is that the operating system takes turns to execute each task alternately. Task 1 executes for 0.01 seconds, then switches to task 2, then switches to task 3 for 0.01 seconds... and so on. On the surface, each task is executed alternately, but because the execution speed of the CPU is so fast, we feel as if all tasks are being executed at the same time.

The real parallel execution of multitasking can only be realized on a multi-core CPU. However, since the number of tasks is far greater than the number of cores of the CPU, the operating system will automatically schedule many tasks to be executed on each core in turn.

For the operating system, a task is a process (Process). For example, opening a browser starts a browser process, opening a notepad starts a notepad process, and opening two notepads starts two notepads In this process, opening a Word starts a Word process.

Some processes do more than one thing at the same time, such as Word, which can perform typing, spell checking, printing and other things at the same time. Inside a process, if you want to do multiple things at the same time, you need to run multiple "subtasks" at the same time. We call these "subtasks" in the process threads (Thread).

Since each process has to do at least one thing, a process has at least one thread. Of course, a complex process like Word can have multiple threads, and multiple threads can execute at the same time. The execution method of multi-threading is the same as that of multiple processes. The threads all alternate briefly, appearing to be executing at the same time. Of course, the real simultaneous execution of multiple threads requires a multi-core CPU to be possible.

All the Python programs we wrote earlier are processes that execute a single task, that is, there is only one thread. What if we want to execute multiple tasks at the same time?

There are two solutions:

One is to start multiple processes. Although each process has only one thread, multiple processes can execute multiple tasks together.
Another method is to start a process and start multiple threads in a process, so that multiple threads can also perform multiple tasks together.
Of course, there is a third method, which is to start multiple processes, and each process starts multiple threads, so that more tasks are executed at the same time. Of course, this model is more complicated and is rarely used in practice.

To sum up, there are three ways to implement multitasking:

1: Multi-process mode;
2: Multi-thread mode;
3: Multi-process + multi-thread mode.

Notice:

1: Concurrency: refers to the number of tasks that exceeds the number of CPU cores. Through various task scheduling algorithms of the operating system, multiple tasks can be executed "together" (in fact, there are always some tasks that are not being executed, because the switching speed of tasks is quite fast. , seem to execute together)

2: Parallel: means that the number of tasks is less than or equal to the number of cpu cores, that is, the tasks are really executed together.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us