# -*- coding: utf-8 -*- from threading import Thread def func(name): print("子线程: %s" %name) if __name__ == ‘__main__‘: t = Thread(target=func, args=("lily",)) t.start() print("主线程") # 子线程: lily # 主线程
# -*- coding: utf-8 -*- from multiprocessing import Process def func(name): print("子进程: %s" %name) if __name__ == ‘__main__‘: p = Process(target=func, args=("lily",)) p.start() # 这里需要资源开销比线程大,所以先打印主进程 print("主进程") # 主进程 # 子进程: lily
第二种开启线程的方法(自定义类,继承Thread类)
# -*- coding: utf-8 -*- from threading import Thread class MyThread(Thread): def run(self): # 必须有的方法 print("子线程: %s" % self.name) if __name__ == ‘__main__‘: t = MyThread() t.start() print("子线程") # 子线程: Thread-1 # 子线程
原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11025214.html
时间: 2024-10-28 07:59:54