线程
https://codeleading.com/article/2672239990/
import threading import time class myThread(threading.Thread): def __init__(self,threadID,name,counter): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.counter = counter def run(self): print('start thread' + self.name) print_time(self.name,self.counter,1) print('exit time'+ self.name) def print_time(threadName,counter,delay): while counter: time.sleep(delay) print(threadName,time.ctime(time.time())) counter -= 1 if __name__ == "__main__": thread1 = myThread(1,'thread1',5) thread2 = myThread(2,'thread2',10) thread1.start() thread2.start() thread1.join() thread2.join() print('exit main')
https://www.zhihu.com/question/492028213/answer/3104145929?utm_id=0
import threading # 继承Thread类创建线程 class MyThread(threading.Thread): def run(self): # 线程执行的代码 print("Hello, World!") # 使用函数创建线程 def my_function(): # 线程执行的代码 print("Hello, World!") # 创建线程对象并启动线程 thread1 = MyThread() thread2 = threading.Thread(target=my_function) thread1.start() thread2.start() 作者:子午 链接:https://www.zhihu.com/question/492028213/answer/3104145929 来源:知乎 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
分类:
并发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术