我正在学习python多处理,这是我的代码:
import multiprocessing as mp
import time
def cube(list_i, result_i):
for i in list_i:
result_i[i]=i**3
return
def test():
result_i=list(range(10000000))
list_i=list(range(10000000))
single=mp.Process(target=cube, args=(range(10000000),result_i))
t=time.time()
single.start()
single.join()
print(time.time()-t)
multiple=[mp.Process(target=cube, args=(i, result_i)) for i in [range(i*2500000, i*2500000+2500000) for i in range(4)]]
t=time.time()
for process in multiple:
process.start()
for process in multiple:
process.join()
print(time.time()-t)
return
if __name__=='__main__':
test()
输出:
12.0096
32.0467
似乎单个过程更快?我的计算机的CPU是i5-5200,带4个内核。
以下是经理的示例。
import multiprocessing as mp
import time
import requests
# for example we will fetch data about countries
def call_url(url: str, result: dict):
response = requests.get(url)
result['data'] = response.json()
def test():
manager = mp.Manager()
# result of process
result = manager.dict()
# send url and result to process
single = mp.Process(
target=call_url,
args=(
'https://restcountries.eu/rest/v2/all',
result,
))
start_time = time.time()
single.start()
# wait when process is finish and print time + result
single.join()
print('nSingle time: %s' % (time.time() - start_time))
print('nSingle result: %s' % result.values())
processes = []
start_time = time.time()
# create 4 processes and fetch data in each process
for i in range(4):
result = manager.dict()
process = mp.Process(
target=call_url,
args=(
'https://restcountries.eu/rest/v2/all',
result,
))
processes.append((process, result, ))
process.start()
# wait until all processes are finished
for process, _ in processes:
process.join()
# print results of each process and time
print('nresults: ')
for _, result in processes:
print('n')
print(result.values())
print('n4 process time: %s' % (time.time() - start_time))
if __name__ == '__main__':
test()
运行我们的脚本(Python 3.6.1
(。输出的示例:
Single time: 0.9279030323028564
Single result: [[{'name': 'Afghanistan'...
results: # long output (the same result as in single - [[{'name': 'Afghanistan'...)
4 process time: 1.0175669193267822
Process finished with exit code 0
因此,您可以看到0.9〜 = 1.0。但是第二次发送了4个请求。另外,您可以使用队列,游泳池等。因此有很多示例(也在Internet中(。
希望这会有所帮助。