zoukankan      html  css  js  c++  java
  • python 进程和队列 简单示例

    import time
    import random
    
    from multiprocessing import Process, Queue, current_process, freeze_support
    
    
    #
    # Function run by worker processes
    #
    
    def worker(input, output):
        for func, args in iter(input.get, 'STOP'):
            result = calculate(func, args)
            output.put(result)
    #
    # Function used to calculate result
    #
    def calculate(func, args):
        result = func(*args)
        return '%s says that %s%s = %s' % 
               (current_process().name, func.__name__, args, result)
    #
    # Functions referenced by tasks
    #
    def mul(a, b):
        time.sleep(0.5 * random.random())
        return a * b
    
    def plus(a, b):
        time.sleep(0.5 * random.random())
        return a + b
    
    def test():
        NUMBER_OF_PROCESSES = 4
        TASKS1 = [(mul, (i, 7)) for i in range(20)]
        TASKS2 = [(plus, (i, 8)) for i in range(10)]
    
        # Create queues
        task_queue = Queue()
        done_queue = Queue()
    
        # Submit tasks
        for task in TASKS1:
            task_queue.put(task)
    
        # Start worker processes
        for i in range(NUMBER_OF_PROCESSES):
            Process(target=worker, args=(task_queue, done_queue)).start()
    
        # Get and print results
        print('Unordered results:')
        for i in range(len(TASKS1)):
            print('	', done_queue.get())
    
        # Add more tasks using `put()`
        # for task in TASKS2:
        #     task_queue.put(task)
    
        # Get and print some more results
        # for i in range(len(TASKS2)):
        #     print('	', done_queue.get())
        #
        # # Tell child processes to stop
        for i in range(NUMBER_OF_PROCESSES):
            task_queue.put('STOP')
    
    if __name__ == '__main__':
        freeze_support()
        test()
    
  • 相关阅读:
    Dubbo 配置参数
    类文件结构

    shell script 编程入门
    OnePlus5刷 TWRP
    TimeUtil 工具类
    Outline 科学的上网
    HotSpot虚拟机的锁优化
    equals与hashCode
    Java中的动态代理
  • 原文地址:https://www.cnblogs.com/qxh-beijing2016/p/14951011.html
Copyright © 2011-2022 走看看