day35-python之协程
1.协程
# import time
# import queue
#
# def consumer(name):
#
# print("--->ready to eat baozi...")
# while True:
# new_baozi = yield
# print("[%s] is eating baozi %s" % (name,new_baozi))
# #time.sleep(1) # def producer():
#
# r = con.__next__()
# # r = con2.__next__()
# #
# # n = 0
# # while 1:
# # time.sleep(1)
# # print("\033[32;1m[producer]\033[0m is making baozi %s and %s" %(n,n+1) )
# # con.send(n)
# # con2.send(n+1)
# # n +=2
# #
# #
# # if __name__ == '__main__':
# #
# # con = consumer("c1")
# # con2 = consumer("c2")
# # producer() # from greenlet import greenlet
#
# def test1():
# print(12)
# gr2.switch()
# print(34)
# def test2():
# print(56)
# gr1.switch()
# print(78)
# gr1.switch()
#
# gr1 = greenlet(test1)
# gr2 = greenlet(test2)
# gr2.switch() # import gevent
# import requests,time
# start=time.time()
# def f(url):
# print('GET: %s' % url)
# resp =requests.get(url)
# data = resp.text
# print('%d bytes received from %s.' % (len(data), url))
#
# f('https://www.python.org/')
# f('https://www.yahoo.com/')
# f('https://www.baidu.com/')
# f('https://www.sina.com.cn/')
# f("http://www.xiaohuar.com/hua/")
#
# # gevent.joinall([
# # gevent.spawn(f, 'https://www.python.org/'),
# # gevent.spawn(f, 'https://www.yahoo.com/'),
# # gevent.spawn(f, 'https://www.baidu.com/'),
# # gevent.spawn(f, 'https://www.sina.com.cn/'),
# # gevent.spawn(f, 'http://www.xiaohuar.com/hua/'),
# # ])
#
# # f('https://www.python.org/')
# #
# # f('https://www.yahoo.com/')
# #
# # f('https://baidu.com/')
#
# # f('https://www.sina.com.cn/')
#
# print("cost time:",time.time()-start)
2.进程同步
# from multiprocessing import Process, Lock
# import time
# # def f(l, i):
#
# l.acquire()
# time.sleep(1)
# print('hello world %s' % i)
# l.release()
#
# if __name__ == '__main__':
# lock = Lock()
#
# for num in range(10):
# Process(target=f, args=(lock, num)).start()
3.进程池
# from multiprocessing import Process,Pool
# import time,os
#
# def Foo(i):
#
# time.sleep(1)
# print(i)
# print("son",os.getpid())
#
# return "HELLO %s"%i
#
from multiprocessing import Process,Pool
# def Bar(arg):
# print(arg)
# # print("hello")
# # print("Bar:",os.getpid())
#
# if __name__ == '__main__':
#
# pool = Pool(5)
# print("main pid",os.getpid())
# for i in range(100):
# #pool.apply(func=Foo, args=(i,)) #同步接口
# #pool.apply_async(func=Foo, args=(i,))
#
# #回调函数: 就是某个动作或者函数执行成功后再去执行的函数
#
# pool.apply_async(func=Foo, args=(i,),callback=Bar)
#
# pool.close()
# pool.join() # join与close调用顺序是固定的
#
# print('end')
4.进程通信
#
#
#
# import queue,time
#
# import multiprocessing
# def foo(q):
# time.sleep(1)
# print("son process",id(q))
# q.put(123)
# q.put("yuan")
# # if __name__ == '__main__':
# #q=queue.Queue()
# q=multiprocessing.Queue()
# p=multiprocessing.Process(target=foo,args=(q,))
# p.start()
# #p.join()
# print("main process",id(q))
# print(q.get())
# print(q.get())
#
#
#
#
#
#
# from multiprocessing import Process, Pipe
# def f(conn):
# conn.send([12, {"name":"yuan"}, 'hello'])
# response=conn.recv()
# print("response",response)
# conn.close()
# print("q_ID2:",id(conn))
# # if __name__ == '__main__':
#
# parent_conn, child_conn = Pipe() #双向管道
#
# print("q_ID1:",id(child_conn))
# p = Process(target=f, args=(child_conn,))
# p.start()
#
# print(parent_conn.recv()) # prints "[42, None, 'hello']"
# parent_conn.send("儿子你好!")
# p.join()
#
#
# from multiprocessing import Process, Manager
#
# def f(d, l,n):
#
# d[n] = '1' #{0:"1"}
# d['2'] = 2 #{0:"1","2":2}
#
# l.append(n) #[0,1,2,3,4, 0,1,2,3,4,5,6,7,8,9]
# #print(l)
# # if __name__ == '__main__':
#
# with Manager() as manager:
#
# d = manager.dict()#{}
#
# l = manager.list(range(5))#[0,1,2,3,4]
#
#
# p_list = []
#
# for i in range(10):
# p = Process(target=f, args=(d,l,i))
# p.start()
# p_list.append(p)
#
# for res in p_list:
# res.join()
#
# print(d)
# print(l)
day35-python之协程的更多相关文章
- python gevent 协程
简介 没有切换开销.因为子程序切换不是线程切换,而是由程序自身控制,没有线程切换的开销,因此执行效率高, 不需要锁机制.因为只有一个线程,也不存在同时写变量冲突,在协程中控制共享资源不加锁,只需要判断 ...
- 深入理解Python中协程的应用机制: 使用纯Python来实现一个操作系统吧!!
本文参考:http://www.dabeaz.com/coroutines/ 作者:David Beazley 缘起: 本人最近在学习python的协程.偶然发现了David Beazley的co ...
- 关于Python的协程问题总结
协程其实就是可以由程序自主控制的线程 在python里主要由yield 和yield from 控制,可以通过生成者消费者例子来理解协程 利用yield from 向生成器(协程)传送数据# 传统的生 ...
- {python之协程}一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二
python之协程 阅读目录 一 引子 二 协程介绍 三 Greenlet 四 Gevent介绍 五 Gevent之同步与异步 六 Gevent之应用举例一 七 Gevent之应用举例二 一 引子 本 ...
- 【Python】协程
协程,又称微线程,纤程.英文名Coroutine. 协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用. 子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在 ...
- Python之协程(coroutine)
Python之协程(coroutine) 标签(空格分隔): Python进阶 coroutine和generator的区别 generator是数据的产生者.即它pull data 通过 itera ...
- python的协程和_IO操作
协程Coroutine: 协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行. 注意,在一个子程序中中断,去执行其他子程序,不是函数调用,有点 ...
- python 3 协程函数
python 3 协程函数 1:把函数的执行结果封装好__iter__和__next__,即得到一个迭代器 2:与return功能类似,都可以返回值,但不同的是,return只能返回一次值,而yiel ...
- Python之协程函数
Python之协程函数 什么是协程函数:如果一个函数内部yield的使用方法是表达式形式的话,如x=yield,那么该函数成为协程函数. def eater(name): print('%s star ...
- 多任务-python实现-协程(2.1.11)
多任务-python实现-协程(2.1.11) 23/100 发布文章 qq_26624329 @ 目录 1.概念 2.迭代器 1.概念 协程与子例程一样,协程(coroutine)也是一种程序组件. ...
随机推荐
- NamedPipeStream的使用案例
NamedPipeStream的使用具体案例如下: using System; using System.Data; using System.Data.SQLite; using System.IO ...
- Android dump命令查看某个apk是被谁安装的?
adb shell dumpsys package packages > packageAll.txt ORadb shell pm dump packages > package ...
- [Bayes] Concept Search and LSI
基于术语关系的贝叶斯网络信息检索模型扩展研究 LSI 阅读笔记 背景知识 提出一种改进的共现频率法,利用该方法挖掘了索引术语之间的相关关系,将这种相关关系引入信念网络模型,提出了一个具有两层术语节点的 ...
- C++使用fill初始化二维数组
类似如下用法: fill(dis[0], dis[0]+maxn*maxn, INF); 因为 dis[0]才是dis的首元素 dis[0][0] 的地址.
- Excel统计发票和金税盘核对新版
之前的博文:如何使用Excel表格状态栏动态查看统计,介绍了如何利用excel一拉就可以进行统计,和金税盘的月度统计统计.由于最近年月日显示成方框,所以作废了发票和对冲了上月的一张发票,导致这个月出现 ...
- linux修改系统时间和时区
1.修改系统时间linux系统时钟有两个,一个是硬件时钟,即BIOS时间,就是我们进行CMOS设置时看到的时间,另一个是系统时钟,是linux系统Kernel时间.当Linux启动时,系统Kernel ...
- [LeetCode] 502. IPO 上市
Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Cap ...
- POJ1191 棋盘分割
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: 题目链接: http://poj.org/problem?id ...
- python局部变量和全局变量(6)
在python开发中,变量也是有生命周期的,一旦周期结束,程序会自动清理暂用的空间,释放内存,变量分为两者,一种是局部变量,一种是全局变量,两者具体有什么区别呢…… 一.局部变量 一般而言在函数内部或 ...
- [转帖]FastDFS图片服务器单机安装步骤
FastDFS图片服务器单机安装步骤 https://www.cnblogs.com/yuesf/p/11847103.html 前面已经讲 一张图秒懂微服务的网络架构,通过此文章可以了解FastDF ...