前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python threading

Python threading

作者头像
py3study
发布2020-01-06 15:35:12
3300
发布2020-01-06 15:35:12
举报
文章被收录于专栏:python3

 1.  第一种方式: 创建一个threading.Thread()的实例,给它一个函数。

import threading

代码语言:javascript
复制
from time import sleep, ctime

loops = [4, 2]

def loop(nloop, nsec):
    print '\nstart loop:', nloop, 'at:', ctime()
    sleep(nsec)
    print '\nloop', nloop, 'done at:', ctime()

def main():
    print '\nstarting at:\n', ctime()
    threads = []
    nloops = range(len(loops))

    #create the threads using threading
    for i in nloops:
         t = threading.Thread(target=loop, args=(i,loops[i]))
         threads.append(t)

    #start threads
    for i in nloops:
        threads[i].start()

    #wait for all
    for i in nloops:
        threads[i].join() #block
        
    print '\nall Done at:', ctime()

if __name__ == '__main__':
    main()

/-----------------------------------------------------------------------------------------------------------------------------------------------------

2. 第二种方式: 创建一个threading.Thread的实例,传给它一个可调用类对象,类中使用__call__()函数调用函数

代码语言:javascript
复制
import threading
from time import sleep, ctime

loops = [4,2]

class ThreadFunc(object):
    def __init__(self, func, args, name=''):
        self.name = name
        self.func = func
        self.args = args
    #when you create a new thread, Thread instance will invoke our ThreadFunc
    # instance, and at that time, it will call the function __call__()
    # Hence, we have a tuple arguments , so we use the self.res
    def __call__(self):
        self.res = self.func(*self.args) #invoke the func

def loop(nloop, nsec):
    print '\nstart loop:', nloop, 'at:', ctime()
    sleep(nsec)
    print '\nloop', nloop, 'done at:', ctime()

def main():
    print 'start at:', ctime()
    threads = [] #list
    nloops = range(len(loops))

    #crate all threads
    for i in nloops:
        t = threading.Thread(target=ThreadFunc(loop, (i, loops[i]),loop.__name__))
        threads.append(t)

    #start all threads
    for i in nloops:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all Done at:', ctime()

if __name__ == '__main__':
    main()
代码语言:javascript
复制
/-----------------------------------------------------------------------------------
代码语言:javascript
复制
3. 第三种方式: 派生一个threading.Thread出一个子类,创建这个子类的实例,使用run调用函数

import threading 
from time import sleep, ctime

loops = (4,2)

class MyThread(threading.Thread):
    def __init__(self, func, args, name=''):
        threading.Thread.__init__(self) #base class func
        self.name = name
        self.func = func
        self.args = args

    # in the other way, use __call__()
    def run(self):
        apply(self.func,self.args)

def loop(nloop, nsec):
    print 'start loop', nloop, 'at:', ctime()
    sleep(nsec)
    print 'loop', nloop, 'done at:', ctime()

def main():
    print 'starting at:',ctime()
    threads = []
    nloops = range(len(loops))

    for i in nloops:
        t = MyThread(loop, (i,loops[i]), loop.__name__)
        threads.append(t)

    for i in nloops:
        threads[i].start()

    for i in nloops:
        threads[i].join()

    print 'all Done at:', ctime()

if __name__ == '__main__':
    main()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档