我有一個程式使用許多對其一般操作很重要的異步函式,但我需要其中一個同時執行 2 個行程。為此,我決定使用多行程,創建并啟動行程 p1 和行程 p2。p1 將是一種計時 8 秒的計時器,這將是行程 p2 必須完成任務的時間限制,但如果 p2 未在 8 秒之前完成其任務(即 p1 在 p2 之前完成)然后兩個行程都關閉,程式繼續。
在這種情況下,p2 是一個對字串進行操作的行程,并對名為 text 的變數的內容進行編碼,完成后將其回傳給主程式。另一方面,p1 是一個行程,其目標是限制行程 p2 必須執行字串操作并將結果加載到文本變數中的時間。
該演算法的目標是避免這樣的情況:如果用戶發送了超過 8 秒的超長操作(或者在可能的情況下系統已經飽和以在合理的時間內處理該資訊),那么 p2 將停止這樣做,變數文本的內容不會改變。
這是我的簡化代碼,我嘗試讓 2 個執行緒(稱為 p1 和 p2),每個執行緒執行不同的函式,但兩個函式都在同一個類中,我從異步函式呼叫這個類:
import asyncio, multiprocessing, time
#class BotBrain(BotModule):
class BotBrain:
# real __init__()
'''
def __init__(self, chatbot: object) -> None:
self.chatbot = chatbot
self.max_learn_range = 4
corpus_name = self.chatbot.config["CorpusName"]
self.train(corpus_name)
'''
# test __init__()
def __init__(self):
self.finish_state = multiprocessing.Event()
def operation_process(self, input_text, text):
#text = name_identificator(input_text, text)
text = "aaa" #for this example I change the info directly without detailing the nlp process
return input_text, text
def finish_process(self, finish_state):
time.sleep(8)
print("the process has been interrupted!")
self.finish_state.set()
def process_control(self, finish_state, input_text, text):
p1 = multiprocessing.Process(target=self.finish_process, args=(finish_state,))
p1.start()
p2 = multiprocessing.Process(target=self.operation_process, args=(input_text, text,))
p2.start()
#close all unfinished processes, p1 or p2
finish_state.wait()
for process in [p1, p2]:
process.terminate()
def process(self, **kwargs) -> str:
input_text, text = "Hello, how are you?", ""
text = self.process_control(self.finish_state, input_text, text) #for multiprocessing code
async def main_call():
testInstance = BotBrain() #here execute the instructions of the __init__() function
testInstance.process()
asyncio.run(main_call())
但它給了我這些錯誤,我不知道如何解決它們:
File "J:\async_and_multiprocess.py", line 46, in process_control
p1.start()
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\process.py", line 121, in start
self._popen = self._Popen(self)
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\context.py", line 224, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\context.py", line 327, in _Popen
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\context.py", line 327, in _Popen
return Popen(process_obj)
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
return Popen(process_obj)
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 45, in __init__
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
prep_data = spawn.get_preparation_data(process_obj._name)
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\spawn.py", line 154, in get_preparation_data
_check_not_importing_main()
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
_check_not_importing_main()
File "C:\Users\MIPC\anaconda3\lib\multiprocessing\spawn.py", line 134, in _check_not_importing_main
raise RuntimeError('''
RuntimeError:
An attempt has been made to start a new process before the
current process has finished its bootstrapping phase.
This probably means that you are not using fork to start your
child processes and you have forgotten to use the proper idiom
in the main module:
if __name__ == '__main__':
freeze_support()
...
The "freeze_support()" line can be omitted if the program
is not going to be frozen to produce an executable.
是什么freeze_support()意思?我應該把它放在課堂上的什么地方?
這是我的程式應該如何作業的流程圖,盡管它沒有考慮 freeze_support() 因為我不知道你的意思。

uj5u.com熱心網友回復:
多處理可能很棘手,部分原因是 Python 的匯入系統。從多處理模塊的編程指南:
確保新的 Python 解釋器可以安全地匯入主模塊,而不會導致意外的副作用(例如啟動新行程)。
這就是這里發生的事情。匯入你的主模塊執行asyncio.run(main_call()),這反過來會產生另一個子行程,依此類推。
您需要通過以下方式防止您asyncio.run在匯入時被執行:
if __name__ == "__main__":
asyncio.run(main_call())
如果您不想生成凍結的二進制檔案(例如使用 pyinstaller),您可以忽略帶有freeze_support().
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/429030.html
標籤:Python 哎呀 多处理 蟒蛇异步 python-多处理
上一篇:Python-描述符-損壞的類
