相关疑难解决方法(0)

如何在Python3中检测concurrent.futures中的异常?

由于其并发期货模块,我刚刚转到python3.我想知道我是否可以让它来检测错误.我想将并发期货用于并行程序,如果有更高效的模块请告诉我.

我不喜欢多处理,因为它太复杂,没有太多的文档.然而,如果有人可以编写一个没有类的Hello World,那么将使用多处理来并行计算,这样很容易理解.

这是一个简单的脚本:

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)
Run Code Online (Sandbox Code Playgroud)

上面的代码只是一个演示,关于CTRL + C如何无法打印satement.

我想要的是能够调用函数是一个错误存在.此错误检测必须来自函数本身.

另一个例子

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)
Run Code Online (Sandbox Code Playgroud)

python multiprocessing python-2.7 python-3.4 concurrent.futures

9
推荐指数
2
解决办法
4780
查看次数

如何停止 ThreadPoolExecutor.map 并按 CTRL-C 退出?

Python 脚本多次执行 IO 密集型函数(数量级:5000 到 75000 之间的任意次数)。通过使用这仍然相当有效

def _iterator(): ...  # yields 5000-75000 different names
def _thread_function(name): ...

with concurrent.futures.ThreadPoolExecutor(max_workers=11) as executor:
    executor.map(_thread_function, _iterator(), timeout=44)
Run Code Online (Sandbox Code Playgroud)

如果用户按下 CTRL-C,它只会弄乱单个线程。我希望它停止启动新线程;并完成当前正在进行的线程或立即终止它们,等等。

我怎样才能做到这一点?

python-multithreading python-3.x

5
推荐指数
1
解决办法
2622
查看次数