【发布时间】:2016-04-09 02:53:09
【问题描述】:
所以我正在尝试使用我在网上找到的这个 sn-p 在后台运行一些代码。然而,我的主要问题是我试图让它在最后不使用 time.sleep(3) 的情况下工作,而且似乎无法弄清楚。有什么建议?为什么它最后只与 time.sleep(n) 一起工作?
import threading
import time
class ThreadingExample(object):
""" Threading example class
The run() method will be started and it will run in the background
until the application exits.
"""
def __init__(self, interval=1):
""" Constructor
:type interval: int
:param interval: Check interval, in seconds
"""
self.interval = interval
thread = threading.Thread(target=self.run, args=())
thread.daemon = True # Daemonize thread
thread.start() # Start the execution
def run(self):
""" Method that runs forever """
while True:
# Do something
print('Doing something imporant in the background')
time.sleep(self.interval)
example = ThreadingExample()
time.sleep(3)
print('Done')
【问题讨论】:
-
看起来你想加入。相关:stackoverflow.com/questions/15085348/…
-
请阅读一些关于 Python 多线程的基础知识。例如:tutorialspoint.com/python/python_multithreading.htm
标签: python multithreading python-multithreading