繁体   English   中英

让一个线程在后台 python 中不断运行

[英]Have a thread constantly run in background python

我有一个函数来检查我想在我的 python 脚本的后台不断运行的嗅探工具:

def check():
    unwanted_programmes = []  # to be added to
    for p in psutil.process_iter(attrs=['pid', 'name']):
        for item in unwanted_programmes:
            if item in str(p.info['name']).lower():
                webhook_hackers(str(p.info['name']))  # send the programme to a webhook which is in another function
                sys.exit()
    time.sleep(1)

我希望它从一开始就运行,然后是脚本的其余部分

我写过这样的代码:

if __name__ == "__main__":
    check = threading.Thread(target=get_hackers())
    check.start()
    check.join()
    threading.Thread(target=startup).start()

# startup function just does some prints and inputs before running other functions 

但是,此代码仅运行一次check然后startup但我希望check运行然后继续在后台运行。 同时, startup只运行一次,我该怎么做?

你的check函数做你想做的事,但它只做一次,这就是你看到的行为; 线程完成运行该函数,然后干净地退出。 如果你把所有东西都放在函数里面的while(True):块在函数的顶部,那么函数将无限循环并且线程永远不会退出,这听起来像你想要的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM