久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <tfoot id='zXhVz'></tfoot>

    1. <legend id='zXhVz'><style id='zXhVz'><dir id='zXhVz'><q id='zXhVz'></q></dir></style></legend>

      <small id='zXhVz'></small><noframes id='zXhVz'>

      • <bdo id='zXhVz'></bdo><ul id='zXhVz'></ul>
      <i id='zXhVz'><tr id='zXhVz'><dt id='zXhVz'><q id='zXhVz'><span id='zXhVz'><b id='zXhVz'><form id='zXhVz'><ins id='zXhVz'></ins><ul id='zXhVz'></ul><sub id='zXhVz'></sub></form><legend id='zXhVz'></legend><bdo id='zXhVz'><pre id='zXhVz'><center id='zXhVz'></center></pre></bdo></b><th id='zXhVz'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='zXhVz'><tfoot id='zXhVz'></tfoot><dl id='zXhVz'><fieldset id='zXhVz'></fieldset></dl></div>

    2. 如何在 Python3 中檢測 concurrent.futures 中的異常?

      How to detect exceptions in concurrent.futures in Python3?(如何在 Python3 中檢測 concurrent.futures 中的異常?)

      <small id='mm2ZK'></small><noframes id='mm2ZK'>

    3. <legend id='mm2ZK'><style id='mm2ZK'><dir id='mm2ZK'><q id='mm2ZK'></q></dir></style></legend>
        <tbody id='mm2ZK'></tbody>

      1. <tfoot id='mm2ZK'></tfoot>

          <i id='mm2ZK'><tr id='mm2ZK'><dt id='mm2ZK'><q id='mm2ZK'><span id='mm2ZK'><b id='mm2ZK'><form id='mm2ZK'><ins id='mm2ZK'></ins><ul id='mm2ZK'></ul><sub id='mm2ZK'></sub></form><legend id='mm2ZK'></legend><bdo id='mm2ZK'><pre id='mm2ZK'><center id='mm2ZK'></center></pre></bdo></b><th id='mm2ZK'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='mm2ZK'><tfoot id='mm2ZK'></tfoot><dl id='mm2ZK'><fieldset id='mm2ZK'></fieldset></dl></div>
              • <bdo id='mm2ZK'></bdo><ul id='mm2ZK'></ul>
                本文介紹了如何在 Python3 中檢測 concurrent.futures 中的異常?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                限時送ChatGPT賬號..

                由于它的并發期貨模塊,我剛剛轉向 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
                

                以下是要點:

                1. 當您使用 with ThreadPoolExecutor(max_workers=3) as exe 時,它會等待所有任務完成.看看 Doc

                1. 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 is False 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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
              • 相關文檔推薦

                What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數傳遞給 pool.map() 函數)
                multiprocessing.pool.MaybeEncodingError: #39;TypeError(quot;cannot serialize #39;_io.BufferedReader#39; objectquot;,)#39;(multiprocessing.pool.MaybeEncodingError: TypeError(cannot serialize _io.BufferedReader object,)) - IT屋-程序員軟件開
                Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?(Python 多進程池.當其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數?)
                yet another confusion with multiprocessing error, #39;module#39; object has no attribute #39;f#39;(與多處理錯誤的另一個混淆,“模塊對象沒有屬性“f)

                <tfoot id='nB20Z'></tfoot>

                <legend id='nB20Z'><style id='nB20Z'><dir id='nB20Z'><q id='nB20Z'></q></dir></style></legend>
                <i id='nB20Z'><tr id='nB20Z'><dt id='nB20Z'><q id='nB20Z'><span id='nB20Z'><b id='nB20Z'><form id='nB20Z'><ins id='nB20Z'></ins><ul id='nB20Z'></ul><sub id='nB20Z'></sub></form><legend id='nB20Z'></legend><bdo id='nB20Z'><pre id='nB20Z'><center id='nB20Z'></center></pre></bdo></b><th id='nB20Z'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='nB20Z'><tfoot id='nB20Z'></tfoot><dl id='nB20Z'><fieldset id='nB20Z'></fieldset></dl></div>
                  <bdo id='nB20Z'></bdo><ul id='nB20Z'></ul>

                  <small id='nB20Z'></small><noframes id='nB20Z'>

                      <tbody id='nB20Z'></tbody>

                          主站蜘蛛池模板: 午夜免费在线电影 | 国产视频中文字幕 | 亚洲 欧美 另类 日韩 | 青青草一区二区三区 | 日本高清视频在线播放 | 国产99久久精品一区二区300 | 亚洲综合在 | 欧美啪啪| 精品一区二区在线看 | 少妇久久久久 | 91精品国产综合久久久久久蜜臀 | 欧美v日韩v | 毛片免费观看 | 少妇久久久久 | 久久精品小视频 | 欧一区二区 | 中文字幕一级 | 久久久精品一区二区三区 | 91佛爷在线观看 | 午夜影院在线观看 | 在线一区 | 日本精品久久 | 久久网站免费视频 | 欧美视频在线播放 | 欧美一级在线观看 | 亚洲最大av | 国产欧美一区二区三区另类精品 | 亚洲综合国产精品 | 亚洲欧美少妇 | 成年人视频在线免费观看 | 欧美激情综合 | av在线免费观看网站 | 亚洲在线免费观看 | 99亚洲 | 欧美综合久久 | 国产午夜精品一区二区三区四区 | 欧美日韩手机在线观看 | www.狠狠操 | 亚洲天堂精品一区 | www.色53色.com | 中文字幕 欧美 日韩 |