Python线程中的死锁 [英] Deadlock in Python Threads

查看:80
本文介绍了Python线程中的死锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python实现一个简单的portscanner.它通过创建许多工作线程来工作,这些工作线程扫描队列中提供的端口.他们将结果保存在另一个队列中.扫描所有端口后,线程和应用程序应终止.问题就在这里:对于少量端口,一切正常,但是,如果我尝试扫描200个或更多端口,则应用程序将陷入僵局.我不知道为什么.

I am trying to implement a simpley portscanner with Python. It works by creating a number of worker threads which scan ports that are provided in a queue. They save the results in another queue. When all ports are scanned the threads and the application should terminate. And here lies the problem: For small numbers of ports everything works fine, but if I try to scan 200 or more ports, the application will get caught in a deadlock. I have no idea, why.

class ConnectScan(threading.Thread):
    def __init__(self, to_scan, scanned):
        threading.Thread.__init__(self)
        self.to_scan = to_scan
        self.scanned = scanned

    def run(self):
        while True:
            try:
                host, port = self.to_scan.get()
            except Queue.Empty:
                break
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            try:
                s.connect((host, port))
                s.close()
                self.scanned.put((host, port, 'open'))
            except socket.error:
                self.scanned.put((host, port, 'closed'))
            self.to_scan.task_done()


class ConnectScanner(object):
    def scan(self, host, port_from, port_to):
        to_scan = Queue.Queue()
        scanned = Queue.Queue()
        for port in range(port_from, port_to + 1):
            to_scan.put((host, port))
        for i in range(20):
            ConnectScan(to_scan, scanned).start()
        to_scan.join()

有人看到有什么问题吗?我还要感谢一些技巧,介绍如何在Python中调试此类线程问题.

Does anybody see what might be wrong? Also I would appreciate some tipps how to debug such threading issues in Python.

推荐答案

to_scan队列中的并非所有项目都已被消耗,并且您没有足够多次调用task_done方法来解除阻止ConnectScanner.

It is likely that not all items on the to_scan queue are consumed and that you're not calling the task_done method enough times to unblock ConnectScanner.

是不是在ConnectScan.run运行时抛出了您没有捕获的异常并且线程过早终止了?

Could it be that an exception is thrown during the runtime of ConnectScan.run that you're not catching and your threads prematurely terminate?

这篇关于Python线程中的死锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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