table of Contents
"Python 3 Programming Guide" study notes
There are two ways to carry out the distribution of the work load, it is to use a multi-process, the other is the use of multi-threading.
10.1 Using multiprocessing module
We can use the Python subprocess module to achieve this need, change module provides functionality to run other programs, can pass we need any command line arguments, and, if desired, may also be used in which the communicating pipe.
#maincontrol.py
import subprocess
import os, sys
def main():
child = os.path.join(os.path.dirname(__file__),
"subcontrol.py")
pipes = []
s = "See you again, Robot {0}"
for i in range(10):
command = [sys.executable, child]
pipe = subprocess.Popen(command, stdin=subprocess.PIPE)
pipes.append(pipe)
pipe.stdin.write(s.format(i).encode("utf-8") + b"\n") #subprocess模块读写的是字节,而并不是字符串
pipe.stdin.close()
while pipes:
print("?????")
pipe = pipes.pop()
pipe.wait()
print("#####")
if __name__ == "__main__":
main()
#subcontrol.py
import sys
sys.stdin = sys.stdin.detach()
stdin = sys.stdin.read()
lines = stdin.decode("utf8", "ignore")
print(lines)
or
#subcontrol.py
import sys
stdin = sys.stdin.buffer.read()
lines = stdin.decode("utf8", "ignore")
print(lines)
The output is:
?????
See you again, Robot 0
See you again, Robot 3
See you again, Robot 1
See you again, Robot 7
See you again, Robot 6
See you again, Robot 9
See you again, Robot 5
See you again, Robot 8
See you again, Robot 2
#####
?????
#####
?????
#####
?????
#####
?????
#####
?????
See you again, Robot 4
#####
?????
#####
?????
#####
?????
#####
?????
#####
Can be found, the process may not be able to create the first complete.
10.2 will work across multiple threads
When multiple threads share data, it may be sold ready-made to the existing data were improper modification occurs, a common solution is to use some sort of locking mechanism. By accessing the shared data is defined within the scope capitulate lock, the shared data can be guaranteed only be accessed by one thread at the same time, even if this protection is not direct.
One problem with the lock mechanism is there is a risk of deadlock. For example, thread # 1 A lock request and requests a lock on this basis B, but does not lock thread # 1 B, because the thread # 2 and a lock B, only when the unlocking thread # 2 B, thread # 1 to Lock B, In case Unfortunately, this time the thread # 2 is also requesting a lock B, then a deadlock occurs, two threads are blocked.
He looked content threads, threading module:
here Wallpaper