[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

Example 5-32. PP4E\System\Processes\multi4.py


"""
Process class can also be subclassed just like threading.Thread;
Queue works like queue.Queue but for cross-process, not cross-thread
"""


import os, time, queue
from multiprocessing import Process, Queue # process-safe shared queue


queue is a pipe + locks/semas


class Counter(Process):
label = ' @'
def init(self, start, queue): # retain state for use in run
self.state = start
self.post = queue
Process.init(self)


def run(self): # run in newprocess on start()
for i in range(3):
time.sleep(1)
self.state += 1
print(self.label ,self.pid, self.state) # self.pid is this child's pid
self.post.put([self.pid, self.state]) # stdout file is shared by all
print(self.label, self.pid, '-')


if name == 'main':
print('start', os.getpid())
expected = 9


post = Queue()
p = Counter(0, post) # start 3 processes sharing queue
q = Counter(100, post) # children are producers
r = Counter(1000, post)
p.start(); q.start(); r.start()


while expected: # parent consumes data on queue
time.sleep(0.5) # this is essentially like a GUI,
try: # though GUIs often use threads
data = post.get(block=False)
except queue.Empty:
print('no data...')
else:
print('posted:', data)
expected -= 1


p.join(); q.join(); r.join() # must get before join putter
print('finish', os.getpid(), r.exitcode) # exitcode is child exit status


Notice in this code how:



  • The time.sleep calls in this code’s producer simulate long-running tasks.

  • All four processes share the same output stream; print calls go the same place and
    don’t overlap badly on Windows (as we saw earlier, the multiprocessing module
    also has a shareable Lock object to synchronize access if required).


The multiprocessing Module| 253
Free download pdf