すべてのブログすべてのモットー:今日と明日を照らしてください。
0.はじめに
このセクションでは、実験を行い、pythonでのマルチプロセスを要約します。
提案: 最初にマルチスレッドの概念を見てください。このセクションでは概念を繰り返しません。クリックしてください
1.テキスト
1.1デコレータ
関数の実行時間を表示するために使用されるデコレータ、クリックしてください
import threading
import time
import functools
from multiprocessing import Process
c = 0
def count_time(func):
"""装饰器:计算程序运行时间"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
t1 = time.time()
res = func(*args, **kwargs)
t2 = time.time()
# 计算时间
time_consumed = t2 - t1
print('{}函数一共花费了{}秒'.format(func.__name__, time_consumed))
return res
return wrapper
1.2マルチプロセステストパート
1.2.1テストされた機能は2つの機能です
テストされた機能は次のとおりです。
@count_time
def mop_floor(arg):
"""拖地操作,"""
global c
print('开始拖地……')
for i in range(3):
time.sleep(1)
c += 5
c -= 5
print('{} is running……c={} !!'.format(arg, c))
print('---------地拖完了!-----------')
@count_time
def heat_water(arg):
"""烧水操作"""
global c
print('我要烧水了……')
for i in range(4):
time.sleep(1)
print('{} is running……c={} !!'.format(arg, c))
print('------------水烧开了!-------------')
1>。単一プロセス
単一プロセス機能:
@count_time
def single_processing():
"""单进程"""
# 对两个函数进行测试
mop_floor('1')
heat_water('2')
print('-' * 100)
主な機能:
def main():
# 单进程
single_processing()
if __name__ == '__main__':
main()
単一プロセスの実行結果:
2>。マルチプロセス
マルチプロセス機能
@count_time
def my_processing():
p1 = Process(target=mop_floor, args=('1',))
p2 = Process(target=heat_water, args=('2',))
p1.start()
p2.start()
p1.join()
p2.join()
# -----------------------------------------
# process_array = {}
# n = 1
# for tid in range(n):
# t = Process(target=single_func_tested, args=('1',))
# t.start()
# process_array[tid] = t
# for i in range(n):
# process_array[i].join()
print('-' * 100)
主な機能
def main():
# 多进程
my_processing()
if __name__ == '__main__':
main()
その結果、合計時間が短縮されていることがわかります。
1.2.2テスト対象の関数は計算関数です
テスト中の機能
@count_time
def single_func_tested(arg):
"""对一个函数进行测试"""
sum = 0
for i in range(10000000): # 100000
sum += i
print('{} over'.format(arg))
1>。単一プロセス
シングルプロセス機能
@count_time
def single_processing():
"""单进程"""
# 对一个函数进行测试
single_func_tested('1')
print('-' * 100)
主な機能:
def main():
# 单进程
single_processing()
# 多进程
# my_processing()
if __name__ == '__main__':
main()
単一プロセスの計算結果:
2>。マルチプロセス
マルチプロセス機能
@count_time
def my_processing():
process_array = {
}
n = 1
for tid in range(n):
t = Process(target=single_func_tested, args=('1',))
t.start()
process_array[tid] = t
for i in range(n):
process_array[i].join()
print('-' * 100)
主な機能:
def main():
# 多进程
my_processing()
if __name__ == '__main__':
main()
n = 1の
場合、n = 2の
場合、n = 3の
場合、n = 4の
場合、n = 5の場合、
要約:
- n <= 4の場合、プログラムの合計実行時間は変化しません。これは、実験マシンに4つのコアがあるため、プロセスが4つ未満の場合はコアで十分です。n> 4の場合、プログラムの実行時間は増加します。開いているプロセスの数がコアの数を超えています。
- プロセスを開くための条件は、複数のCPUであるか、1つのCPUに複数のコアがあります
- 単一のプログラムのマルチプロセスは、プログラムの実行時間を短縮することはありません。単一のプログラムをさらに数回実行するだけです。
1.2.3テストされている機能は待機機能です
テスト中の機能
@count_time
def mop_floor(arg):
"""拖地操作,"""
global c
print('开始拖地……')
for i in range(3):
time.sleep(1)
c += 5
c -= 5
print('{} is running……c={} !!'.format(arg, c))
print('---------地拖完了!-----------')
1>。単一プロセス
シングルプロセス機能
@count_time
def single_processing():
"""单进程"""
# 对两个函数进行测试
mop_floor('1')
# heat_water('2')
# 对一个函数进行测试
# single_func_tested('1')
print('-' * 100)
主な機能
def main():
# 单进程
single_processing()
if __name__ == '__main__':
main()
結果:
2>。マルチプロセス
マルチプロセス機能:
@count_time
def my_processing():
n = 1
for tid in range(n):
t = Process(target=mop_floor, args=('1',))
t.start()
process_array[tid] = t
for i in range(n):
process_array[i].join()
print('-' * 100)
主な機能:
def main():
# 多进程
my_processing()
if __name__ == '__main__':
main()
n = 1の
場合、n = 2の
場合、n = 3の
場合、n = 4の
場合、n = 5の
場合、n = 8の場合、
要約:
- プロセス理論がコア理論を超えると、合計実行時間はわずかに増加し、その増加は計算関数ほど明白ではありません。
- 単一のプログラムのマルチプロセスは、プログラムの実行時間を短縮することはありません。単一のプログラムをさらに数回実行するだけです。
上記。
総括する:
- 複数のプロセスを開くための条件は、複数のCPUまたは1つのマルチコアCPUです。
- 1つのプログラムに対して複数のプロセスを開く、効果なし
1.3ソースコード
https://gist.github.com/onceone/f5489dcc1499c81ee0161f0ea3bb442b
参照
[1] https://blog.csdn.net/weixin_39190382/article/details/107107980
[2] http://www.uml.org.cn/python/201901221.asp
[3] https://www.jianshu .com / p / 644dbb6d4cc8
[4] https://blog.csdn.net/m0_37324740/article/details/85765167
[5] https://www.cnblogs.com/-qing-/p/11291581.html
[6 ] https://blog.csdn.net/lzy98/article/details/88819425
[7] https://www.cnblogs.com/yssjun/p/11302500.html
[8] https://www.jb51.net /article/167165.htm
[9] https://blog.csdn.net/weixin_44850984/article/details/89165731
[10] https://www.cnblogs.com/justbreaking/p/7218909.html?utm_source=itdadao&utm_medium =紹介