线程

 

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
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
复制代码

 

posted @   liushao-AI  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示