python 协程 demo
# -*- coding: UTF- -*- import gevent
from gevent import socket
from gevent import event rev=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
rev.bind(('',))
ip=[]
cur= def preload():
for i in open('ip'):
ip.append(i)
print("load "+str(len(ip))+"ip") def send_request(data):
global cur
ret=rev.sendto(data,(ip[cur],))
cur=(cur+)%len(ip) class Cache:
def __init__(self):
self.c={}
def get(self,key):
return self.c.get(key,None)
def set(self,key,value):
self.c[key]=value
def remove(self,key):
self.c.pop(key,None) cache=Cache() def handle_request(s,data,addr):
req=dnslib.DNSRecord.parse(data)
qname=str(req.q.qname)
qid=req.header.id
ret=cache.get(qname)
if ret:
ret=dnslib.DNSRecord.parse(ret)
ret.header.id=qid;
s.sendto(ret.pack(),addr)
else:
e=event.Event()
cache.set(qname+"e",e)
send_request(data)
e.wait()
tmp=cache.get(qname)
if tmp:
tmp=dnslib.DNSRecord.parse(tmp)
tmp.header.id=qid;
s.sendto(tmp.pack(),addr) def handle_response(data):
req=dnslib.DNSRecord.parse(data)
qname=str(req.q.qname)
print(qname)
cache.set(qname,data)
e=cache.get(qname+"e")
cache.remove(qname+"e")
if e:
e.set()
e.clear() def handler(s,data,addr):
req=dnslib.DNSRecord.parse(data)
if req.header.qr:
handle_response(data)
else:handle_request(s,data,addr) def main():
preload()
while True:
data,addr=rev.recvfrom()
gevent.spawn(handler,rev,data,addr) if __name__ == '__main__':
main()
python 协程 demo的更多相关文章
- 带你简单了解python协程和异步
带你简单了解python的协程和异步 前言 对于学习异步的出发点,是写爬虫.从简单爬虫到学会了使用多线程爬虫之后,在翻看别人的博客文章时偶尔会看到异步这一说法.而对于异步的了解实在困扰了我好久好久,看 ...
- python协程详解,gevent asyncio
python协程详解,gevent asyncio 新建模板小书匠 #协程的概念 #模块操作协程 # gevent 扩展模块 # asyncio 内置模块 # 基础的语法 1.生成器实现切换 [1] ...
- Python 协程总结
Python 协程总结 理解 协程,又称为微线程,看上去像是子程序,但是它和子程序又不太一样,它在执行的过程中,可以在中断当前的子程序后去执行别的子程序,再返回来执行之前的子程序,但是它的相关信息还是 ...
- day-5 python协程与I/O编程深入浅出
基于python编程语言环境,重新学习了一遍操作系统IO编程基本知识,同时也学习了什么是协程,通过实际编程,了解进程+协程的优势. 一.python协程编程实现 1. 什么是协程(以下内容来自维基百 ...
- 终结python协程----从yield到actor模型的实现
把应用程序的代码分为多个代码块,正常情况代码自上而下顺序执行.如果代码块A运行过程中,能够切换执行代码块B,又能够从代码块B再切换回去继续执行代码块A,这就实现了协程 我们知道线程的调度(线程上下文切 ...
- 从yield 到yield from再到python协程
yield 关键字 def fib(): a, b = 0, 1 while 1: yield b a, b = b, a+b yield 是在:PEP 255 -- Simple Generator ...
- 关于python协程中aiorwlock 使用问题
最近工作中多个项目都开始用asyncio aiohttp aiomysql aioredis ,其实也是更好的用python的协程,但是使用的过程中也是遇到了很多问题,最近遇到的就是 关于aiorwl ...
- 用yield实现python协程
刚刚介绍了pythonyield关键字,趁热打铁,现在来了解一下yield实现协程. 引用官方的说法: 与线程相比,协程更轻量.一个python线程大概占用8M内存,而一个协程只占用1KB不到内存.协 ...
- [转载] Python协程从零开始到放弃
Python协程从零开始到放弃 Web安全 作者:美丽联合安全MLSRC 2017-10-09 3,973 Author: lightless@Meili-inc Date: 2017100 ...
随机推荐
- 理解Sql Server 事务隔离层级(Transaction Isolation Level)
关于Sql Server 事务隔离级别,百度百科是这样描述的 隔离级别:一个事务必须与由其他事务进行的资源或数据更改相隔离的程度.隔离级别从允许的并发副作用(例如,脏读或虚拟读取)的角度进行描述. 隔 ...
- OLED屏幕那些次像素有趣的排列方式
http://www.dzsc.com/data/2016-6-2/109856.html 我们今天的重点内容为倒数第二列内容的上半部分,也就是RGB排列和Pentile排列.在介绍OLED屏幕时候我 ...
- 10大H5前端框架 ......>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
作为一名做为在前端死缠烂打6年并且懒到不行的攻城士,这几年我还是阅过很多同门从知名到很知名的各种前端框架,本来想拿15-20个框架来分享一下,但在跟几个前辈讨教写文章的技巧时果断被无情的打击了,所以这 ...
- 基于SSL的MySQL主从
master 端 配置CA和证书 [root@baseos-1_192.168.31.140 ~]# cd /etc/pki/CA/ #生成根证书的私钥 [root@baseos-1_192.168. ...
- windows composer 安装,使用新手入门
一.前期准备: 1.下载安装包,https://getcomposer.org/download/ 2.在php.ini文档中打开extension=php_openssl.dll 3.下载php_s ...
- ql常见面试题 受用了
//转至:http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921911.html 1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名 ...
- 【题解】Luogu P4344 [SHOI2015]脑洞治疗仪
原题传送门:P4344 [SHOI2015]脑洞治疗仪 前置芝士:珂朵莉树 窝博客里对珂朵莉树的介绍 没什么好说的自己看看吧 珂朵莉树好题啊 我一开始一直Re65 后来重构代码就ac了,或许是rp问题 ...
- 推荐 Net C# 逆向反编译四大工具利器
参考:https://blog.csdn.net/kongwei521/article/details/54927689/
- Codeforces 799D Field expansion - 搜索 - 贪心
In one of the games Arkady is fond of the game process happens on a rectangular field. In the game p ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...