python中的进程+协程

# encoding:utf-8

from multiprocessing import Value,Queue,Process
import gevent
import time
from testapi import test
from gevent import monkey;monkey.patch_all()

inq = Queue()
outq = Queue()
end = Value('d', 1)
thds = []

def xiechengfun(i):
    try:
        while 1:
            try:
                task = inq.get(timeout=0.1)
            except Exception as e:
                print e
                break
            re = test()
            outq.put(str(re))
            print('Worker %s got task %s' % (i, task))
            #用于协程之间的切换
            #gevent.sleep(0)
    except Exception as e:
        print 'Quitting time!'

def inputs():
    for i in xrange(100):
        inq.put(i)
        time.sleep(0)

def save():pass

def xiecheng():
    threads = [gevent.spawn(xiechengfun, i) for i in xrange(10)]
    gevent.joinall(threads)

def is_end():pass

if __name__ == "__main__":
    x = Process(target=inputs, args=())
    x.start()
    y = Process(target=save, args=())
    y.start()
    for i in xrange(1):
        p = Process(target=xiecheng, args=())
        thds.append(p)
        p.start()
        print "p.pid:", p.pid
        print "p.name:", p.name
        print "p.is_alive:", p.is_alive()

    while 1:
        if is_end():
            end.value = 0
            break
        time.sleep(1)

你可能感兴趣的:(python中的进程+协程)