美文网首页
python中的进程+协程

python中的进程+协程

作者: 晨暮云海 | 来源:发表于2017-05-26 10:32 被阅读0次
# 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笔记3

    python 无线程池 ,有进程池 阻塞 意外着等待子进程结束 字典的无序性 : 进程,线程,协程 协程,又称微...

  • Gevent高并发网络库精解

    进程 线程 协程 异步 并发编程(不是并行)目前有四种方式:多进程、多线程、协程和异步。 多进程编程在python...

  • python中的进程+协程

  • python 多进程+协程 爬虫

    多进程 关于多进程爬虫和多进程方面的知识可以参考崔庆才的博客和python文档 协程 关于协程的介绍,强烈推荐大家...

  • Python-02进阶-04多进程多线程

    Python 进阶-04 进程线程协程并发等.md tags: Python 多进程 并发 进阶 必备知识 201...

  • 2018-11-22进程,线程,协程

    进程:代码+资源,可以实现多任务线程:运行在进程中的最小单元,消耗资源小于进程 可以实现多任务协程:Python独...

  • 协程

    协程 1.Kotlin 中的协程是什么? 简单理解:一般程序会有一个主进程,主进程中可能含有多个线程。而协程,是线...

  • python多线程、多进程、协程的使用

    python多线程、多进程、协程的使用 本文主要介绍多线程、多进程、协程的最常见使用,每个的详细说明与介绍有时间会...

  • python异步协程(aiohttp,asyncio)

    python异步协程 环境:python3.7.0 协程 协程,英文叫做 Coroutine,又称微线程,纤程,协...

  • GO学习笔记(18) - 并发编程(1) - 理解gorouti

    目录 进程、线程与协程 并发模模型介绍 GO并发编程介绍 进程、线程与协程 进程和线程 进程是程序在操作系统中的一...

网友评论

      本文标题:python中的进程+协程

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