如何在Python3中的current.futures中检测异常? [英] How to detect exceptions in concurrent.futures in Python3?

查看:117
本文介绍了如何在Python3中的current.futures中检测异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于并发的Futures模块,我刚开始使用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时,它将等待直到完成所有任务.看看文档

  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,则此方法将立即返回,并且当所有未决的Future完成执行时,与执行程序关联的资源将被释放.不管wait的值如何,整个Python程序只有在所有未完成的future完成执行后才会退出.

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(等待,就像在设置为True的情况下调用Executor.shutdown()一样)

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块并设置quit=True.然后您的辅助线程可以退出.

    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中的current.futures中检测异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆