multiprocessing
conceptual
Basic Flow of Creating Multiple Processes
Creating process objects
Starting processes Recycling processes
Code:
import multiprocessing as mp from time import sleep # Process execution functions def fun(): print("Start a process") sleep(3) print("End of process") # Create process objects p = (target = fun) () # Start the process () # Recovery process
Run results:
Starting a process
process termination
Process finished with exit code 0
1, the parent and child processes are executed in parallel child process execution function parent process execution in addition to the contents of the child process:
import multiprocessing as mp from time import sleep # Process execution functions def fun(): print("Start a process") sleep(3) print("End of process") # Create process objects p = (target = fun) # Make the fun function a separate child process Other functions are executed by the process. () # Start the process sleep(2) print("What the parent process executes") () # Recovery process print("===============") """ pid = if pid == 0 fun() os._exit(0) else: () """
Run results:
Starting a process
What the parent process executes
process termination
===============
2, the child process can not change the value of variables in the parent process
Code:
import multiprocessing as mp from time import sleep a = 1 # Process execution functions def fun(): print("Start a process") sleep(3) global a print("a=", a) a = 1000 print("a=", a) print("End of process") # Create process objects p = (target = fun) # Make the fun function a separate child process Other functions are executed by the process. () # Start the process sleep(2) print("What the parent process executes") () # Recovery process print("===============") print("a=", a)
Run results:
Starting a process
What the parent process executes
a= 1
a= 1000
process termination
a= 1
Creating multiple processes
Code:
""" Creating multiple processes """ from multiprocessing import Process import os from time import sleep def fun1(): sleep(2) print((), '--', (), "Eat.") def fun2(): sleep(3) print((), '--', (), "Sleep.") def fun3(): sleep(4) print((), '--', (), "Learning.") jobs =[] for th in [fun1, fun2, fun3]: p = Process(target = th) (p) () for i in jobs: ()
Run results:
46013 - 46022 Eating
46013 - 46023 Sleeping
46013 - 46024 Study
Process functions with parameters
Code:
from multiprocessing import Process from time import sleep # Process functions with parameters def worker(sec, name): for i in range(3): sleep(sec) print("I'm %s"%name) print("I'm working...") # p = Process(target = worker, args = (2, "Tom")) p = Process(target = worker, kwargs = {'name':'tom', 'sec': 2}) () ()
Run results:
I'm tom
I'm working…
I'm tom
I'm working…
I'm tom
I'm working…
Case Exercise
Code:
from multiprocessing import Process import os filename = './' size = (filename) # Copy the top half def up(): fr = open(filename, 'rb') fw = open('bot,jpg', 'wb') n = size//2 ((n)) () () # Copy the second half def down(): fr = open(filename, 'rb') fw = open('bot,jpg', 'wb') (size//2.0) (()) () () p = Process(target = up) q = Process(target = down) () () () ()
To this point this article summarizes python multiprocessing multiprocessing knowledge of the article is introduced to this, more related python multiprocessing multiprocessing content please search for my previous articles or continue to browse the following related articles I hope you will support me more in the future!