如何从导入的库中线程化函数?

时间:2016-09-07 14:02:09

标签: python python-multithreading

我一直在研究Python程序,但Python的语法对我来说还是比较新的。例如,如果我有两个Python文件,其中包含以下内容,那么线程函数的正确方法是什么?

main.py - 这是正在执行的文件

#!/usr/bin/env python

#threading array
threads = []

import threading
import anotherFile as af

afCheck = af.init() #return true or false
if afCheck == True: 
    threads.append(threading.Thread(target=af.listener))

try:
    for thread in threads:
        thread.start()
except (KeyboardInterrupt, SystemExit):
    print("closing up shop");
    for thread in threads:
        thread.cancel()
    sys.exit()

我打算为anotherFile.py编写一些虚拟代码,但这看起来非常无关紧要。我已经能够测试这一点,我已经看到它没有执行监听器,我不确定正确的方法来调用它。

也许我只需要将侦听器线程化为另一个.File.py,但是为了简单起见,将它们全部集中在一个地方会很好,因为这将有多个导入。

0 个答案:

没有答案
相关问题