python 分布式进程

实现过程

1.建立对象Queue
2.将队列注册到网络
3.实例化对象绑定端口和authkey
4.启动示例
5.通过网络访问Queue
6.创建任务上传网络Queue

Demo

# -*- coding: utf-8 -*-
from multiprocessing.managers import BaseManager
import time, Queue

#1
task_queue = Queue.Queue()
result_queue = Queue.Queue()
def get_task():
    return task_queue
def get_result():
    return result_queue

class Quequemanager(BaseManager):
    pass

def run():
    #2
    Quequemanager.register('get_task_queue', callable=get_task)
    Quequemanager.register('get_result_queue', callable=get_result)

    #3
    manager = Quequemanager(address=('127.0.0.1', 8001), authkey='qiye')

    #4
    manager.start()

    try:
        # 5
        task = manager.get_task_queue()
        result = manager.get_result_queue()
        #创建任务
        for url in ["ImageUrl_"+str(i) for i in range(1,10)]:
            task.put(url)
            print "put task %s ..." %url
        print "try get result ..."
        for i in range(10):
            print "result is %s " %result.get(timeout=10)
        print 'end'
    except:
        pass
    finally:
        manager.shutdown


if __name__ == '__main__':
    run()

你可能感兴趣的:(python 分布式进程)