from multiprocessing import Process
def print_func(continent='Asia'):
print('The name of continent is : ', continent)
if __name__ == "__main__": # confirms that the code is under main function
names = ['America', 'Europe', 'Africa']
procs = []
proc = Process(target=print_func) # instantiating without any argument
procs.append(proc)
proc.start()
# instantiating process with arguments
for name in names:
# print(name)
proc = Process(target=print_func, args=(name,))
procs.append(proc)
proc.start()
# complete the processes
for proc in procs:
proc.join()
Process对象
带参数
需要通过关键字args传递
启动
虽然定义了Process obj, 但是启动该Process需要调用start( ), 否则什么也不做。 当调用start之后,process开始run,并且返回结果
结束
上述代码的输出:
The name of continent is : Asia
The name of continent is : America
The name of continent is : Europe
The name of continent is : Africa
from multiprocessing import Queue
colors = ['red', 'green', 'blue', 'black']
cnt = 1
# instantiating a queue object
queue = Queue()
print('pushing items to queue:')
for color in colors:
print('item no: ', cnt, ' ', color)
queue.put(color)
cnt += 1
print('\npopping items from queue:')
cnt = 0
while not queue.empty():
print('item no: ', cnt, ' ', queue.get())
cnt += 1
输出如下:
pushing items to queue:
item no: 1 red
item no: 2 green
item no: 3 blue
item no: 4 black
popping items from queue:
item no: 0 red
item no: 1 green
item no: 2 blue
item no: 3 black
import multiprocessing
import time
def task1(lock):
with lock: # with context using lock, release lock automatically
n = 5
while n > 1:
print(f"{time.strftime('%H:%M:%S')} task1 output")
time.sleep(1)
n -= 1
def task2(lock):
lock.acquire()
n = 5
while n > 1:
print(f"{time.strftime('%H:%M:%S')} task2 output")
time.sleep(1)
n -= 1
lock.release()
def task3(lock):
lock.acquire()
n = 5
while n > 1:
print(f"{time.strftime('%H:%M:%S')} task3 output")
time.sleep(1)
n -= 1
lock.release()
if __name__ == "__main__":
lock = multiprocessing.Lock()
p1 = multiprocessing.Process(target=task1, args=(lock,))
p2 = multiprocessing.Process(target=task2, args=(lock,))
p3 = multiprocessing.Process(target=task3, args=(lock,))
p1.start()
p2.start()
from multiprocessing import Lock, Process, Queue, current_process
import time
import queue # imported for using queue.Empty exception
def do_job(tasks_to_accomplish, tasks_that_are_done):
while True:
try:
'''
try to get task from the queue. get_nowait() function will
raise queue.Empty exception if the queue is empty.
queue(False) function would do the same task also.
'''
print('get task')
task = tasks_to_accomplish.get_nowait()
except queue.Empty:
print('queue empty')
break
else:
'''
if no exception has been raised, add the task completion
message to task_that_are_done queue
'''
print(task)
tasks_that_are_done.put(task + ' is done by ' + current_process().name)
time.sleep(.5)
return True
def main():
number_of_task = 10
number_of_processes = 4
tasks_to_accomplish = Queue()
tasks_that_are_done = Queue()
processes = []
for i in range(number_of_task):
tasks_to_accomplish.put("Task no " + str(i))
# creating processes
for w in range(number_of_processes):
p = Process(target=do_job, args=(tasks_to_accomplish, tasks_that_are_done))
processes.append(p)
p.start()
# completing process
for p in processes:
p.join()
# print the output
while not tasks_that_are_done.empty():
print(tasks_that_are_done.get())
return True
if __name__ == '__main__':
main()
输出:
Task no 0
Task no 1
Task no 2
Task no 3
Task no 4
Task no 5
Task no 6
Task no 7
Task no 8
Task no 9
Task no 0 is done by Process-1
Task no 2 is done by Process-3
Task no 1 is done by Process-2
Task no 3 is done by Process-4
Task no 4 is done by Process-2
Task no 6 is done by Process-1
Task no 5 is done by Process-3
Task no 7 is done by Process-4
Task no 8 is done by Process-2
Task no 9 is done by Process-3
参考:
Python多进程之Process、Pool、Lock、Queue、Event、Semaphore、Pipe
Python多线程详解
Python之多进程与多线程
Python Multiprocessing Example