pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

      <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="cao6s64" 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="weeoo4i" 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="gmwui8s" 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>

                          主站蜘蛛池模板: 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 郑州律师咨询-郑州律师事务所_河南锦盾律师事务所 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 紧急泄压人孔_防爆阻火器_阻火呼吸阀[河北宏泽石化] | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 沈阳建筑设计公司_加固改造设计_厂房设计_设计资质加盟【金辉设计】 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 工业制氮机_psa制氮机厂家-宏骁智能装备科技江苏有限公司 | 山东PE给水管厂家,山东双壁波纹管,山东钢带增强波纹管,山东PE穿线管,山东PE农田灌溉管,山东MPP电力保护套管-山东德诺塑业有限公司 | CPSE安博会 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 自清洗过滤器,浅层砂过滤器,叠片过滤器厂家-新乡市宇清净化 | 钢骨架轻型板_膨石轻型板_钢骨架轻型板价格_恒道新材料 | 机床导轨_导轨板_滚轮导轨-上海旻佑精密机械有限公司 | 洁净棚-洁净工作棚-无菌室-净化工程公司_北京卫护科技有限公司 | uv固化机-丝印uv机-工业烤箱-五金蚀刻机-分拣输送机 - 保定市丰辉机械设备制造有限公司 | 防水套管厂家-柔性防水套管-不锈钢|刚性防水套管-天翔管道 | 浙江建筑资质代办_二级房建_市政_电力_安许_劳务资质办理公司 | 丹尼克尔拧紧枪_自动送钉机_智能电批_柔性振动盘_螺丝供料器品牌 | 通风天窗,通风气楼,屋顶通风天窗,屋顶通风天窗公司 | 分子蒸馏设备(短程分子蒸馏装置)_上海达丰仪器 | 知网论文检测系统入口_论文查重免费查重_中国知网论文查询_学术不端检测系统 | 升降机-高空作业车租赁-蜘蛛车-曲臂式伸缩臂剪叉式液压升降平台-脚手架-【普雷斯特公司厂家】 | 超声波破碎仪-均质乳化机(供应杭州,上海,北京,广州,深圳,成都等地)-上海沪析实业有限公司 | 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 乙炔气体报警装置|固定式氯化氢检测仪|河南驰诚电气百科 | 德国EA可编程直流电源_电子负载,中国台湾固纬直流电源_交流电源-苏州展文电子科技有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 钢托盘,钢制托盘,立库钢托盘,金属托盘制造商_南京飞天金属制品实业有限公司 | 深圳侦探联系方式_深圳小三调查取证公司_深圳小三分离机构 | PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 吉林污水处理公司,长春工业污水处理设备,净水设备-长春易洁环保科技有限公司 | 【ph计】|在线ph计|工业ph计|ph计厂家|ph计价格|酸度计生产厂家_武汉吉尔德科技有限公司 | 牛奶检测仪-乳成分分析仪-北京海谊 | 办公室家具_板式办公家具定制厂家-FMARTS福玛仕办公家具 | DWS物流设备_扫码称重量方一体机_快递包裹分拣机_广东高臻智能装备有限公司 | 冷水机,风冷冷水机,水冷冷水机,螺杆冷水机专业制造商-上海祝松机械有限公司 |