首页 > 编程语言 > 详细

python多线程的两种写法

时间:2019-02-03 20:16:52      阅读:352      评论:0      收藏:0      [点我收藏+]

1.一般多线程

import threading


def func(arg):
    # 获取当前执行该函数的线程的对象
    t = threading.current_thread()
    # 根据当前线程对象获取当前线程名称
    name = t.getName()
    print(name, arg)


for i in range(5):
    t1 = threading.Thread(target=func, args=(i,))
    t1.setName('线程:%s-->' % i)
    t1.start()
    
print('end')

2.面向对象版多线程

class MyThread(threading.Thread):

    def run(self):
        # 获取当前执行该函数的线程的对象
        t = threading.current_thread()
        # 根据当前线程对象获取当前线程名称
        name = t.getName()
        print(name, self._args, self._kwargs)


for i in range(5):
    t = MyThread(args=(i,))
    t.start()

print('end')

python多线程的两种写法

原文:https://www.cnblogs.com/apollo1616/p/10350894.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!