擅长:python、mysql、java
<p>格式很好的问题,有参考资料。谢谢。在</p>
<p>答案。我使用``线程。线程(在这种情况下,目标=…`。但是,您也可以使用继承:</p>
<pre><code>import logging
import threading
import time
def task(name):
log = logging.getLogger('task-' + name)
log.info("Starting up Outlook watcher\n"
"To terminate the program, press 'Ctrl + C'")
while True:
log.info("Doing work that takes time")
time.sleep(1)
class OtlkThread(threading.Thread):
def __init__(self, *args, **kwargs):
self.log = logging.getLogger('task-class')
super().__init__(*args, **kwargs)
def run(self):
self.log.info("Starting up Outlook watcher\n"
"To terminate the program, press 'Ctrl + C'")
while True:
self.log.info("Doing work that takes time")
time.sleep(1)
def main():
t1 = threading.Thread(target=task, args=('daemon',), daemon=True)
t1.start()
t2 = OtlkThread()
t2.start()
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG,)
main()
while True:
time.sleep(1)
</code></pre>