python:线程在守护程序中无法正常工作

丹尼洛·古里亚诺夫(Danylo Gurianov)

我正在编写用于检查SSH连接的简单脚本,但我不明白为什么它会挂在一个线程上。

class myThread(threading.Thread):
    def __init__(self, hostname ):
         threading.Thread.__init__(self)
         self.hostname = hostname
    def run(self):
        return self.doSSH(self.hostname)

    def doSSH(self,hostname):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((hostname, 22))
        result = s.recv(1024)
        if re.findall(r'^SSH.+?SSH.+',result) :
            return "Up"
        else :
            return "Down"

def main():
    q = Queue.Queue()
    completeHostlist = ["host1","host2","google.com","host3"]
    for hostname in completeHostlist:
        thread =  myThread(hostname)
        thread.daemon = True
        q.put_nowait(thread.run())
    q.get_nowait()

我不明白为什么该脚本会挂在google.com上?我期望它产生守护程序线程并继续执行host3。一旦完成host3,它必须杀死与Google的线程并返回结果。我做错了什么?

我已经弄清楚了run()和start()。无论如何,这无法按预期工作,在启动所有host [1-3]线程之后,脚本被google困在了线程中,等待其结束。应该在脚本结尾处将其杀死吗?

我应该使用多处理而不是多线程来为每个主机生成单独的进程吗?

杰夫斯

不要.run()为任何线程直接调用方法。正如@Sorin所说,请致电thread.start()

您无需定义新的线程类,在这种情况下,一个函数就足够了:

from Queue import Queue
from threading import Thread

def is_ssh_up(result_queue, hostname, port=22):
    # try to connect here
    # ...
    # write results
    result_queue.put((hostname, True)) # Up

def main():
    q = Queue()
    hosts = ["host1", "host2", "google.com", "host3"]

    for hostname in hosts: # start worker threads
        t = Thread(target=is_ssh_up, args=[q, hostname])
        t.daemon = True
        t.start()

    for _ in hosts: # collect results
        hostname, is_up = q.get()
        print("%s is %s" % (hostname, "Up" if is_up else "Down"))

或者您可以使用线程池:

from multiprocessing.pool import ThreadPool

def is_ssh_up(hostname, port=22):
    # try to connect here
    # ...
    # return results
    return hostname, is_up

hosts = ["host1", "host2", "google.com", "host3"]
pool = ThreadPool(20) # limit number of concurrent connections to 20
for hostname, is_up in pool.imap_unordered(is_ssh_up, hosts):
    status = "Up" if is_up else "Down" if is_up is not None else "Unknown"
    print("%s status is %s" % (hostname, status))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python服务器守护程序无法正常工作

来自分类Dev

Linux守护程序无法正常工作

来自分类Dev

以“ docker run daemon”启动的守护程序无法正常工作

来自分类Dev

Redis守护程序脚本无法正常工作

来自分类Dev

程序无法正常工作-Python

来自分类Dev

Python 程序无法正常工作

来自分类Dev

Python Selenium多线程无法正常工作

来自分类Dev

python:无法使线程类实例正常工作

来自分类Dev

BeautifulSoup无法在Python的多线程程序中工作

来自分类Dev

Python线程脚本无法正常启动/正常工作

来自分类Dev

Docker守护程序无法正常重启

来自分类Dev

Namenode守护程序无法正常启动

来自分类Dev

在守护程序模式下登录的金字塔uWSGI无法正常工作

来自分类Dev

Docker:尽管启用了守护程序,但用户名称空间重映射无法正常工作

来自分类Dev

无法让这个python程序正常工作

来自分类Dev

Python Palindrome程序无法正常工作

来自分类Dev

C程序中的strcat无法正常工作

来自分类Dev

无限循环在程序中无法正常工作

来自分类Dev

Android中的TouchEvent程序无法正常工作

来自分类Dev

睡眠线程无法正常工作

来自分类Dev

侧面多线程中的SystemFileWatcher无法正常工作

来自分类Dev

python中的线程锁无法按需工作

来自分类Dev

如果PYTHON中的功能无法正常工作

来自分类Dev

Python中的Foreach无法正常工作

来自分类Dev

打印命令无法正常工作在python中

来自分类Dev

如果PYTHON中的功能无法正常工作

来自分类Dev

While循环在Python中无法正常工作

来自分类Dev

Python中的Foreach无法正常工作

来自分类Dev

python中的此功能无法正常工作