問題描述
函數(shù)apply_async
在Python 3 中具有參數(shù)error_callback
.但是Python 2 中沒有這個(gè)論點(diǎn).
Function apply_async
of multiprocessing.Pool
class has argument error_callback
in Python 3. But this argument is missing in Python 2.
有什么技巧可以在 Python 2 中實(shí)現(xiàn)相同的功能嗎?理想情況下,我想編寫在 Python 2 和 3 中運(yùn)行的代碼.
Is there any trick to achieve the same functionality in Python 2 ? Ideally I would like to write code which runs in both Python 2 and 3.
推薦答案
我還沒試過python3.但對我來說,為了捕捉子進(jìn)程中的錯(cuò)誤,我將在子進(jìn)程中運(yùn)行的函數(shù)放在一個(gè)
I haven't tried python3 yet. But for me, to catch the errors in the child process, I put the function that runs in child process within a
import traceback
try:
your code that can make error
except Exception as e:
print e
return False, traceback.format_exc()
else:
return True, result
這樣我會知道是否有問題.
So that I will know if something goes wrong.
我將返回格式更改為 OP 的注釋,以便子進(jìn)程返回一個(gè)元組 (is_success, result or error traceback message)
I change the return format as OP's comment so that the child process returns a tuple (is_success, result or error traceback message )
這樣主進(jìn)程將首先讀取標(biāo)志is_success
,然后相應(yīng)地處理第二個(gè)參數(shù).
So that main process will first read the flag is_success
and then handles the second argument accordingly.
這篇關(guān)于多處理中的error_callback.Python 2中的池apply_async?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!