import threading
import time
from datetime import datetime
class ThreadClient(threading.Thread):
def __init__(self,Name):
threading.Thread.__init__(self)
self._stopper = threading.Event()
self.Name=Name
def stop(self):
self._stopper.set()
def stopped(self):
return self._stopper.isSet()
def run(self):
print("The Thread:"+self.Name+" Started")
while True:
if self.stopped():
print("the Thread:"+self.Name+" Stoped")
return
print("The Thread:"+self.Name+" Output:"+ datetime.now().strftime("%Y-%m-%d:%H:%M:%S"))
time.sleep(1)
if __name__ == "__main__":
ThreadClient1=ThreadClient("Client1")
ThreadClient1.start()
try:
while True:
print("the main:"" Output:"+datetime.now().strftime("%Y-%m-%d:%H:%M:%S"))
time.sleep(2)
except KeyboardInterrupt:
ThreadClient1.stop()
ThreadClient1.join()
finally:
print("Stoped")
Python 多线程编程测试
最新推荐文章于 2024-03-01 13:42:55 发布