Python线程的两种创建方法

来源:互联网 发布:sql 约束类型 编辑:程序博客网 时间:2024/05/18 17:59

第一种:

__author__ = 'LL_YING'import threading# 通过继承Thread创建类class newThread(threading.Thread):    def __init__(self, num):        threading.Thread.__init__(self)        self.num = num    def run(self):        print("I am", self.num)t1 = newThread(1)t1.start()
第二种:

__author__ = 'LL_YING'import threading# 使用threading.Thread直接在线程中运行函数def run(x, y):    for i in range(x, y):        print(i)t1 = threading.Thread(target=run, args=(15, 20)) # 直接使用Thread附加函数,args为参数t1.start()

0 0
原创粉丝点击