简体   繁体   English

Python:线程执行时间

[英]Python: Thread Execution time

Is it possible to set execution time for thread in Python.???? 是否可以在Python中设置线程的执行时间。 like,If that time period get completed I can stop that thread and create new instance of it. 例如,如果该时间段完成,则可以停止该线程并创建该线程的新实例。

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes

    # print "[0, 1, 4,..., 81]"
    print pool.map(f, range(10))

    # print same numbers in arbitrary order
    for i in pool.imap_unordered(f, range(10)):
        print i

    # evaluate "f(20)" asynchronously
    res = pool.apply_async(f, (20,))      # runs in *only* one process
    print res.get(timeout=1)              # prints "400"

From the multiprocessing docs . 来自多处理文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM