美文网首页
1.__call__方法的有意思的运用

1.__call__方法的有意思的运用

作者: 李晓否 | 来源:发表于2017-10-21 11:34 被阅读0次
#!/usr/bin/env python

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

    def __call__(self):
        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:        # create all threads
        t = threading.Thread(
            target=ThreadFunc(loop, (i, loops[i]),
            loop.__name__))
        threads.append(t)

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

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

    print 'all DONE at:', ctime()

if __name__ == '__main__':
    main()

相关文章

  • 1.__call__方法的有意思的运用

  • python内置方法

    1.__call__ 对象通过提供__call__(self, *args ,**kwargs)方法可以模拟函数的...

  • JavaScript map方法的运用

    array 对象的map()方法 currentValue 当前函数值 过程:对array进行遍历,从0到arra...

  • 有意思的存钱方法

    有些姐妹觉得存钱是一件非常难的事情,常常月光、月月光,今天给大姐分享比较有意思的存钱方法: 1、月份倒数法 个人认...

  • 猎场中的女性形象

    运用女性主义的方法

  • Promise方法运用

    本章涉及以下几点 如何创建一个Promise对象 参数讲解 链式调用 catch()捕捉异常 Promise.al...

  • SAS运用方法

    1,产业相关分析法的应用:SAS 2,SAS program 应用方法操作步骤 首先去中国国家统计年鉴和韩国银行经...

  • window.onload的运用方法

    我们继续说JS,我们常常在页面加载完成以后做一些操作,比如一些元素的显示与隐藏、一些动画效果。我们通常有两种方法来...

  • Python函数/方法的高级运用

    Python中,通过 关键字 def 引入一个函数的定义。在函数名后接“()”放置函数的输入参数。小括号后面接“:...

  • 合理运用惩戒的教育方法

    教育既需要“母性之爱”,也需要“父性之爱”。对学生的批评、惩戒,是一种教育的手段和方法,也是一种爱的表达。 该如何...

网友评论

      本文标题:1.__call__方法的有意思的运用

      本文链接:https://www.haomeiwen.com/subject/dcliuxtx.html