1.Queue:
-linux:
q = multiprocessing.Queue()
def task(arg,q):
q.put(arg)
def run():
for i in range(10):
p = multiprocessing.Process(target=task, args=(i, q,))
p.start()
while True:
v = q.get()
print(v)
run()
-windows:
import multiprocessing
def task(arg,q):
q.put(arg)
if __name__ == '__main__':
q = multiprocessing.Queue()
for i in range(10):
p = multiprocessing.Process(target=task,args=(i,q,))
p.start()
while True:
v = q.get()
print(v)
2.Manager:(*)
-Linux:
m = multiprocessing.Manager()
dic = m.dict()
def task(arg):
dic[arg] = 100
def run():
for i in range(10):
p = multiprocessing.Process(target=task, args=(i,))
p.start()
input('>>>')
print(dic.values())
if __name__ == '__main__':
run()
-windows:
import time
import multiprocessing
def task(arg, dic):
time.sleep(2)
dic[arg] = 100
if __name__ == '__main__':
m = multiprocessing.Manager()
dic = m.dict()
process_list = []
for i in range(10):
p = multiprocessing.Process(target=task, args=(i, dic,))
p.start()
process_list.append(p)
while True:
count = 0
for p in process_list:
if not p.is_alive():
count += 1
if count == len(process_list):
break
print(dic)
3.其他电脑
def task(arg,dic):
pass
if __name__ == '__main__':
while True:
# 连接上指定的服务器
# 去机器上获取url
url = 'adfasdf'
p = multiprocessing.Process(target=task, args=(url,))
p.start()
原文地址:https://www.cnblogs.com/apollo1616/p/10351475.html
时间: 2024-10-04 22:46:27