問題描述
由于它的并發期貨模塊,我剛剛轉向 python3.我想知道是否可以讓它檢測錯誤.我想使用并發期貨來并行程序,如果有更高效的模塊請告訴我.
I have just moved on to python3 as a result of its concurrent futures module. I was wondering if I could get it to detect errors. I want to use concurrent futures to parallel program, if there are more efficient modules please let me know.
我不喜歡多處理,因為它太復雜而且沒有多少文檔可用.但是,如果有人可以編寫一個沒有類的 Hello World,只使用多處理并行計算的函數,這樣它就很容易理解了,那就太好了.
I do not like multiprocessing as it is too complicated and not much documentation is out. It would be great however if someone could write a Hello World without classes only functions using multiprocessing to parallel compute so that it is easy to understand.
這是一個簡單的腳本:
from concurrent.futures import ThreadPoolExecutor
def pri():
print("Hello World!!!")
def start():
try:
while True:
pri()
except KeyBoardInterrupt:
print("YOU PRESSED CTRL+C")
with ThreadPoolExecutor(max_workers=3) as exe:
exe.submit(start)
以上代碼只是一個演示,說明 CTRL+C 如何無法打印語句.
The above code was just a demo, of how CTRL+C will not work to print the statement.
我想要的是能夠調用函數是存在錯誤.這種錯誤檢測必須來自函數本身.
What I want is to be able to call a function is an error is present. This error detection must be from the function itself.
另一個例子
import socket
from concurrent.futures import ThreadPoolExecutor
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def con():
try:
s.connect((x,y))
main()
except: socket.gaierror
err()
def err():
time.sleep(1)
con()
def main():
s.send("[+] Hello")
with ThreadPoolExecutor as exe:
exe.submit(con)
推薦答案
這里的解決方案.我不確定你是否喜歡它,但我想不出其他的.我已經修改了您的代碼以使其正常工作.
Here's a solution. I'm not sure you like it, but I can't think of any other. I've modified your code to make it work.
from concurrent.futures import ThreadPoolExecutor
import time
quit = False
def pri():
print("Hello World!!!")
def start():
while quit is not True:
time.sleep(1)
pri()
try:
pool = ThreadPoolExecutor(max_workers=3)
pool.submit(start)
while quit is not True:
print("hei")
time.sleep(1)
except KeyboardInterrupt:
quit = True
以下是要點:
當您使用
with ThreadPoolExecutor(max_workers=3) as exe
時,它會等待所有任務完成.看看 Doc
When you use
with ThreadPoolExecutor(max_workers=3) as exe
, it waits until all tasks have been done. Have a look at Doc
如果 wait
為 True,則此方法將不會返回,直到所有未決的期貨都執行完畢并且與執行程序關聯的資源已被釋放.如果 wait 為 False
,則此方法將立即返回,并且當所有未決的期貨執行完畢后,與執行程序關聯的資源將被釋放.無論 wait 的值如何,整個 Python 程序都不會退出,直到所有未決的期貨都執行完畢.
If
wait
is True then this method will not return until all the pending futures are done executing and the resources associated with the executor have been freed. If wait isFalse
then this method will return immediately and the resources associated with the executor will be freed when all pending futures are done executing. Regardless of the value of wait, the entire Python program will not exit until all pending futures are done executing.
如果您使用 with
語句,您可以避免顯式調用此方法,該語句將關閉 Executor
(就像 Executor.shutdown() 一樣等待
被調用,等待設置為 True
)
You can avoid having to call this method explicitly if you use the with
statement, which will shutdown the Executor
(waiting as if Executor.shutdown()
were called with wait set to True
)
這就像在線程上調用 join()
.
這就是為什么我將其替換為:
It's like calling join()
on a thread.
That's why I replaced it with:
pool = ThreadPoolExecutor(max_workers=3)
pool.submit(start)
主線程必須在做工作"才能捕捉到 Ctrl+C.所以你不能把主線程放在那里然后退出,最簡單的方法是運行一個無限循環
Main thread must be doing "work" to be able to catch a Ctrl+C. So you can't just leave main thread there and exit, the simplest way is to run an infinite loop
現在你已經在主線程中運行了一個循環,當你按下 CTRL+C
時,程序將進入 except KeyboardInterrupt
塊并設置 退出=真
.然后你的工作線程就可以退出了.
Now that you have a loop running in main thread, when you hit CTRL+C
, program will enter the except KeyboardInterrupt
block and set quit=True
. Then your worker thread can exit.
嚴格來說,這只是一種解決方法.在我看來,這不可能有其他方法.
Strictly speaking, this is only a workaround. It seems to me it's impossible to have another way for this.
編輯
我不確定是什么在困擾您,但您可以毫無問題地在另一個線程中捕獲異常:
Edit
I'm not sure what's bothering you, but you can catch exception in another thread without problem:
import socket
import time
from concurrent.futures import ThreadPoolExecutor
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def con():
try:
raise socket.gaierror
main()
except socket.gaierror:
print("gaierror occurred")
err()
def err():
print("err invoked")
time.sleep(1)
con()
def main():
s.send("[+] Hello")
with ThreadPoolExecutor(3) as exe:
exe.submit(con)
輸出
gaierror occurred
err invoked
gaierror occurred
err invoked
gaierror occurred
err invoked
gaierror occurred
...
這篇關于如何在 Python3 中檢測 concurrent.futures 中的異常?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!