gevent

Python-协程

风格不统一 提交于 2020-01-20 04:14:24
一、引子 本节的主题是基于单线程来实现并发,即只用一个主线程(很明显可利用的cpu只有一个)情况下实现并发,为此我们需要先回顾下并发的本质: 切换+保存状态 。 cpu正在运行一个任务,会在两种情况下切走去执行其他的任务(切换由操作系统强制控制): 一种情况是该任务发生了阻塞; 另外一种情况是该任务计算的时间过长或有一个优先级更高的程序替代了它。 协程本质上就是一个线程,以前线程任务的切换是由操作系统控制的,遇到I/O自动切换,现在我们用协程的目的就是较少操作系统切换的开销(开关线程,创建寄存器、堆栈等,在他们之间进行切换等),在我们自己的程序里面来控制任务的切换。 ps:在介绍进程理论时,提及进程的三种执行状态,而线程才是执行单位,所以也可以将上图理解为线程的三种状态 需要 注意 的是: 一:其中第二种情况并不能提升效率,只是为了让cpu能够雨露均沾,实现看起来所有任务都被“同时”执行的效果,如果多个任务都是纯计算的,这种切换反而会降低效率。为此我们可以基于yield来验证。yield本身就是一种在单线程下可以保存任务运行状态的方法,我们来简单复习一下: #1 yiled 可以保存状态,yield的状态保存与操作系统的保存线程状态很像,但是yield是代码级别控制的,更轻量级 #2 send 可以把一个函数的结果传给另外一个函数,以此实现单线程内程序之间的切换 通过yield实现

生成器迭代器正则视频笔记

僤鯓⒐⒋嵵緔 提交于 2020-01-17 08:57:30
多任务文件夹: import os os.mkdir(old_name+"复件") 创建文件夹 os.listdir("dirname") 返回一个列表 multiprocessing 多进程 创建进程池: po = multiprocessing.Pool(5) for work in works: po.apply_async(函数地址,函数参数) multiprocessing.Queue() print("\r %.2f %% " % args, end="") \r 回到行首 %.2f 保留两位小数 %% 显示% end=""不换行 协程: 迭代器: 只能往前不会回退。 for in 元祖, 列表 , 字典 ,字符串 判断是不是可以迭代对象: collections import Iterable isinstance(Object, Iterable) print("xxxx",isinstance(classmate,Iterable)) 打印元祖 必须有__iter__(self): def 保证一个对象成为一个可迭代对象. 保证类里有__iter__(self): 方法 from collections import Iterable class Classmate(object): def __init__(self): self.name = list()

Gevent ready memcache client?

 ̄綄美尐妖づ 提交于 2020-01-15 10:57:28
问题 I see that there's a port of memcache client for gevent (https://github.com/hjlarsson/gevent-memcache), but it has not been updated since August 2010. Do you guys have any recommendation on a memcache client for using in gevent? 回答1: Found one, released and maintained by the guys at www.esn.me https://github.com/esnme/ultramemcache They also have a faster JSON decoder as well, coded in C with python binding. 回答2: There is the python-ultramemcache that give the same interface as python

协程

时光总嘲笑我的痴心妄想 提交于 2020-01-15 04:08:31
python之协程 一. 协程的概念 二. 简单案例 三. 使用协程模块 - greenlet 四. 使用协程模块 - gevent 五. 给程序打补丁 一. 协程的概念 协程叫做微线程,它是python中实现多任务的方式之一。它比线程更小,占用更小的执行单元,它自带cpu上下文。 线程操作消耗性能,而协程的切换不那么消耗性能。 二. 简单案例 实现一个简单的协程: 使用yield关键字让函数变成生成器,再使用next关键字对其进行唤醒。 # -*- coding:utf-8 -*- import time def work1 ( ) : while True : print ( "work" ) yield time . sleep ( 0.5 ) def work2 ( ) : while True : print ( "work2" ) yield time . sleep ( 0.5 ) def main ( ) : w1 = work1 ( ) w2 = work2 ( ) while True : next ( w1 ) next ( w2 ) if __name__ == '__main__' : main ( ) 结果:(循环) work work2 work work2 . . . . . . . . . . . . 三. 使用协程模块 - greenlet 安装

Python并发编程协程(Coroutine)之Gevent

南楼画角 提交于 2020-01-13 03:04:17
转载自https://www.cnblogs.com/zhaof/p/7536569.html event官网文档地址: http://www.gevent.org/contents.html 基本概念 我们通常所说的协程Coroutine其实是corporate routine的缩写,直接翻译为协同的例程,一般我们都简称为协程。 在linux系统中,线程就是轻量级的进程,而我们通常也把协程称为轻量级的线程即微线程。 进程和协程 下面对比一下进程和协程的相同点和不同点: 相同点: 我们都可以把他们看做是一种执行流,执行流可以挂起,并且后面可以在你挂起的地方恢复执行,这实际上都可以看做是continuation,关于这个我们可以通过在linux上运行一个hello程序来理解: shell进程和hello进程: 开始,shell进程在运行,等待命令行的输入 执行hello程序,shell通过系统调用来执行我们的请求,这个时候系统调用会讲控制权传递给操作系统。操作系统保存shell进程的上下文,创建一个hello进程以及其上下文并将控制权给新的hello进程。 hello进程终止后,操作系统恢复shell进程的上下文,并将控制权传回给shell进程 shell进程继续等待下个命令的输入 当我们挂起一个执行流的时,我们要保存的东西: 栈, 其实在你切换前你的局部变量

I can't install Gevent

自古美人都是妖i 提交于 2020-01-12 15:01:18
问题 I need to install Gevent for python2.7 but after try almost all I still doesn't install it. I have python 2.6.6 and here all work ok... but I need python2.7+ then I install python 2.7.9 and now have only problems... Before some part of my project work in python 2.6 ok, but now my project doesn't run witn 2.6 and 2.7, dunno why it's stop working with 2.6 but nvm I need it with 2.7 but.... I cant instal gevent, I have installed libevent-devel an greenlet but I can't instal gevent # pip2.7

Django/gevent socket.IO with redis pubsub. Where do I put things?

前提是你 提交于 2020-01-12 04:00:08
问题 I have an isolated python script that simply captures data from Twitter's streaming API and then on the receipt of each message, using redis pubsub it publishes to the channel "tweets". Here is that script: def main(): username = "username" password = "password" track_list = ["apple", "microsoft", "google"] with tweetstream.FilterStream(username, password, track=track_list) as stream: for tweet in stream: text = tweet["text"] user = tweet["user"]["screen_name"] message = {"text": text, "user"

pymongo + gevent: throw me a banana and just monkey_patch?

旧城冷巷雨未停 提交于 2020-01-11 18:52:41
问题 Quickie here that needs more domain expertise on pymongo than I have right now: Are the "right" parts of the pymongo driver written in python for me to call gevent monkey_patch() and successfully alter pymongo's blocking behavior on r/w within gevent "asynchronous" greenlets? If this will require a little more leg work on gevent and pymongo -- but it is feasible -- I would be more than willing to put in the time as long as i can get a little guidance over irc. Thanks! Note: At small scale

pymongo + gevent: throw me a banana and just monkey_patch?

泪湿孤枕 提交于 2020-01-11 18:52:12
问题 Quickie here that needs more domain expertise on pymongo than I have right now: Are the "right" parts of the pymongo driver written in python for me to call gevent monkey_patch() and successfully alter pymongo's blocking behavior on r/w within gevent "asynchronous" greenlets? If this will require a little more leg work on gevent and pymongo -- but it is feasible -- I would be more than willing to put in the time as long as i can get a little guidance over irc. Thanks! Note: At small scale

线程,进程和协程

六月ゝ 毕业季﹏ 提交于 2020-01-11 15:30:06
python线程 Threading用于特工线程相关的操作,线程是应用程序中工作最小的单元。 1 import threading 2 import time 3 def run(arg): 4 time.sleep(1) 5 print("Hello" + str(arg)) 6 for i in range(10): 7 t = threading.Thread(target = run,args = (i,)) 8 t.start() 9 print("main thread stop") 10 11 12 13 14 15 16 17 ###执行结果,切记线程是无序的。 18 main thread stop 19 Hello0 20 Hello3 21 Hello4 22 Hello2 23 Hello1 24 Hello8 25 Hello7 26 Hello6 27 Hello5 28 Hello9 View Code 上述代码创建了10个“前台”,线程,然后控制器交给你了CPU,CPU根据指定算法进行调度,分片执行指令。 更多方法: 1:start  线程准备就绪,等待CPU调度。 2:setName  为线程设置名称 3:getName  获取线程名称 4:setDaemon  设置为后台线程或前台线程(默认)   如果是后台线程,主线程执行过程中