久久久久久久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 中的異常?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                限時送ChatGPT賬號..

                由于它的并發(fā)期貨模塊,我剛剛轉(zhuǎn)向 python3.我想知道是否可以讓它檢測錯誤.我想使用并發(fā)期貨來并行程序,如果有更高效的模塊請告訴我.

                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.

                我不喜歡多處理,因為它太復(fù)雜而且沒有多少文檔可用.但是,如果有人可以編寫一個沒有類的 Hello World,只使用多處理并行計算的函數(shù),這樣它就很容易理解了,那就太好了.

                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.

                我想要的是能夠調(diào)用函數(shù)是存在錯誤.這種錯誤檢測必須來自函數(shù)本身.

                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)
                

                推薦答案

                這里的解決方案.我不確定你是否喜歡它,但我想不出其他的.我已經(jīng)修改了您的代碼以使其正常工作.

                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. 當(dāng)您使用 with ThreadPoolExecutor(max_workers=3) as exe 時,它會等待所有任務(wù)完成.看看 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,則此方法將不會返回,直到所有未決的期貨都執(zhí)行完畢并且與執(zhí)行程序關(guān)聯(lián)的資源已被釋放.如果 wait 為 False,則此方法將立即返回,并且當(dāng)所有未決的期貨執(zhí)行完畢后,與執(zhí)行程序關(guān)聯(lián)的資源將被釋放.無論 wait 的值如何,整個 Python 程序都不會退出,直到所有未決的期貨都執(zhí)行完畢.

                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 語句,您可以避免顯式調(diào)用此方法,該語句將關(guān)閉 Executor(就像 Executor.shutdown() 一樣等待 被調(diào)用,等待設(shè)置為 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)

                這就像在線程上調(diào)用 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.所以你不能把主線程放在那里然后退出,最簡單的方法是運行一個無限循環(huán)

              • 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

                現(xiàn)在你已經(jīng)在主線程中運行了一個循環(huán),當(dāng)你按下 CTRL+C 時,程序?qū)⑦M入 except KeyboardInterrupt 塊并設(shè)置 退出=真.然后你的工作線程就可以退出了.

                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.

                嚴(yán)格來說,這只是一種解決方法.在我看來,這不可能有其他方法.

                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
                ...
                

                這篇關(guān)于如何在 Python3 中檢測 concurrent.futures 中的異常?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                What exactly is Python multiprocessing Module#39;s .join() Method Doing?(Python 多處理模塊的 .join() 方法到底在做什么?)
                Passing multiple parameters to pool.map() function in Python(在 Python 中將多個參數(shù)傳遞給 pool.map() 函數(shù))
                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 多進程池.當(dāng)其中一個工作進程確定不再需要完成工作時,如何退出腳本?) - IT屋-程序員
                How do you pass a Queue reference to a function managed by pool.map_async()?(如何將隊列引用傳遞給 pool.map_async() 管理的函數(shù)?)
                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>

                          主站蜘蛛池模板: 91最新视频 | 成人免费激情视频 | 日韩欧美三区 | 欧美久久视频 | 久久久久久久国产精品 | 欧美黄网站| 日韩av不卡在线观看 | 国产精品久久久久久久久借妻 | 国产在线视频一区二区 | 性大毛片视频 | 国产高潮在线观看 | 国产精品99久久久久久久久久久久 | 午夜性福利 | 97视频免费观看 | 日韩久久久久久 | 久草中文在线 | 在线视频一区二区三区 | 婷婷久久综合 | 五月天婷婷影院 | 深夜福利网址 | 91在线视频播放 | 国产成人小视频 | av一二三区 | 久久99视频| 国产日韩一区二区三区 | 欧美性爽 | 欧美精品国产 | 欧美黄色一级大片 | 欧美激情一区二区三区 | 久久精品视频一区二区 | 播播激情网| 国产欧美视频在线观看 | 香蕉视频在线免费看 | 久久综合热 | 国产一级片免费看 | 免费成人深夜夜国外 | 日韩精品在线视频 | 中国av在线 | 成 人 黄 色 片 在线播放 | 91色交视频 | 国产乱码一区二区三区 |