[英]Python concurrency with imported modules
為什么導入本地模塊會導致Concurrent.futures.ProcessPoolExecutor引發BrokenProcessPool異常?
導入本地模塊時,運行文件時出現BrokenProcessPool異常。 我嘗試注釋掉該模塊中的所有內容,但得到的結果相同。 我也嘗試過其他具有相同結果的文件/模塊。 但是,如果我注釋了import語句,或將其放置在main()函數中,則它可以在不終止進程和引發異常的情況下工作。 我在其他本地模塊上嘗試了相同的操作,但得到的結果相同。 為什么會發生這種情況,我該怎么做才能避免出現異常情況?
我正在嘗試與ProcessPoolExecutor一起使用current.futures。 我的代碼示例基於以下問題的最高答案: pandas groupby之后並行應用
這是我的版本:
import pandas as pd
import numpy as np
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
import analysis_helper # a local module
print(__name__)
nrows = 15000
np.random.seed(1980)
df = pd.DataFrame({'a': np.random.permutation(np.arange(nrows))})
def f1(group):
time.sleep(0.0001)
return group
def main():
with ProcessPoolExecutor(12) as ppe:
futures = []
results = []
for name, group in df.groupby('a'):
p = ppe.submit(f1, group)
futures.append(p)
for future in as_completed(futures):
r = future.result()
results.append(r)
df_output = pd.concat(results)
print(df_output)
if __name__ == '__main__':
main()
刪除了analysis_helper的結果:
runfile('C:/dev/.../test_parallelizer_pandas.py', wdir='C:/dev/...')
__main__
a
1255 1733
3372 11015
5318 4571
7076 14510
10545 10749
3340 483
11844 3736
3681 14509
2222 1041
3640 11014
4288 7852
12257 1040
2101 11034
14938 3065
8449 1842
7231 10746
7509 4353
4898 3797
2941 866
7497 14520
8302 11013
13882 9924
12007 1042
1567 10747
13135 7856
7742 485
13709 12571
1946 11012
5634 7848
7044 4354
...
3441 14213
179 14361
6723 12134
7528 5905
9273 12420
9916 3614
134 10166
11654 5854
11848 12133
14055 4278
6100 14360
726 14981
13139 14982
12552 14983
5393 14984
6927 14986
8108 14985
12665 14987
8587 14988
11437 14989
4191 14990
6877 14991
4997 14994
13527 14995
9477 14993
2930 14996
5456 14992
781 14997
3287 14998
13386 14999
[15000 rows x 1 columns]
使用analysis_helper的結果:
runfile('C:/dev/.../test_parallelizer_pandas.py', wdir='C:/dev/...')
__main__
Traceback (most recent call last):
File "<ipython-input-7-7d6a88ec5a87>", line 1, in <module>
runfile('C:/dev/.../test_parallelizer_pandas.py', wdir='C:/dev/...')
File "C:\Users\david\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\david\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/dev/.../test_parallelizer_pandas.py", line 42, in <module>
main()
File "C:/dev/.../test_parallelizer_pandas.py", line 35, in main
r = future.result()
File "C:\Users\david\Anaconda3\lib\concurrent\futures\_base.py", line 425, in result
return self.__get_result()
File "C:\Users\david\Anaconda3\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.
注意:這僅在ProcessPoolExecutor中發生,而不在ThreadPoolExecutor中發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.