路飞学城Python-Day32【小结】
import socket
from multiprocessing import Process
def talk(conn):
while True:
try:
data = conn.recv(1024)
if not data:break
conn.send(data.upper())
except ConnectionError:
break
conn.close() def server():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(('127.0.0.1', 8080))
server.listen(5)
while True:
conn, addr = server.accept()
p = Process(target=talk, args=(conn,))
p.start()
server.close() if __name__ == '__main__':
server()
(并发版)服务端
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8080))
while True:
msg = input('>>>>').strip()
if not msg:continue
client.send(msg.encode('gbk'))
data = client.recv(1024)
print(data.decode('gbk'))
(并发版)客户端
from multiprocessing import Process
import time
import random def task(n):
time.sleep(random.randint(1,3))
print('-------->%s' % n) if __name__ == '__main__':
p1=Process(target=task,args=(1,))
p2=Process(target=task,args=(2,))
p3=Process(target=task,args=(3,)) p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
print('-------->4') # 效果二:保证最后输出-------->4
#
# -------->2
# -------->3
# -------->1
# -------->4
join练习题
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8080))
while True:
msg = input('>>>').strip()
if not msg:break
client.send(msg.encode('gbk'))
data = client.recv(1024)
print(data.decode('gbk'))
client.close()
多线程实现并发的套接字通信(客户端)
import socket
from threading import Thread def server(ip, port):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((ip, port))
server.listen(5) while True:
conn, addr = server.accept()
t = Thread(target=comm, args=(conn,))
t.start()
server.close() def comm(conn):
while True:
try:
data = conn.recv(1024)
if not data:break
conn.send(data.upper())
except ConnectionError:
break
conn.close() if __name__ == '__main__':
server('127.0.0.1', 8080)
多线程实现并发的套接字通信(服务端)
import socket
from threading import Thread,currentThread
def client():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8080))
while True:
client.send(('%s hello'%currentThread().getName()).encode('gbk'))
data = client.recv(1024)
print(data.decode('gbk'))
client.close()
if __name__ == '__main__':
for i in range(500):
t = Thread(target=client)
t.start()
基于gevent模块实现并发的套接字通信(客户端)
import socket
from gevent import monkey,spawn;monkey.patch_all()
def comm(conn):
while True:
try:
data = conn.recv(1024)
if not data:break
conn.send(data.upper())
except ConnectionResetError:
break
conn.close() def server(ip,port):
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((ip, port))
server.listen(5)
while True:
conn, addr = server.accept()
spawn(comm, conn)
server.close() if __name__ == '__main__':
g = spawn(server('127.0.0.1', 8080))
g.join()
基于gevent模块实现并发的套接字通信(服务端)
import socket
from threading import Thread,currentThread
def client():
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8080))
while True:
client.send(('%s hello'%currentThread().getName()).encode('gbk'))
data = client.recv(1024)
print(data.decode('gbk'))
client.close()
if __name__ == '__main__':
for i in range(1):
t = Thread(target=client)
t.start()
非阻塞IO模型(客户端)
import socket server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1',8080))
server.listen(5)
server.setblocking(False)
print('starting.....')
rlist = []
while True:
try:
conn, addr = server.accept()
rlist.append(conn)
except BlockingIOError:
del_rlist = []
for conn in rlist:
try:
data = conn.recv(1024)
if not data:
del_rlist.append(conn)
continue
except BlockingIOError:
continue
except Exception:
conn.close()
del_rlist.append(conn)
for conn in del_rlist:
rlist.remove(conn) # while True:
# try:
# data = conn.recv(1024)
# if not data:break
# conn.send(data.upper())
# except ConnectionResetError:
# break
# conn.close()
server.close()
非阻塞IO模型(服务端)
# 开启子进程的目的是执行一个任务
# 方式一
from multiprocessing import Process
import time def task(name):
print('%s is running' % name)
time.sleep(4)
print('%s is done' % name)
# windows操作系统一定要在main下执行Process
if __name__ == '__main__':
p1 = Process(target=task, args=('panda',)) #实例化一个对象
p2 = Process(target=task, kwargs={'name':'boy'}) #实例化一个对象
p1.start() #仅仅是给操作系统发送了一个信号
p2.start()
print('主进程结束')
# --------------------------------------------------------------------------
# 方式二
# 改写默认的一个类,提供功能
from multiprocessing import Process
import time
class MyProcess(Process):
def __init__(self, name):
super().__init__()
self.name = name def run(self):
print('%s is running' % self.name)
time.sleep(4)
print('%s is done' % self.name)
if __name__ == '__main__':
p1 = MyProcess('panda')
p1.start()
print('主进程已经结束了')
开启子进程的两种方式
# join方法
# 如果主进程需要等待子进程结束以后才能运行,就需要join方法
# from multiprocessing import Process
# import time
# import os
# def task():
# print('%s is running.Parent is is %s' % (os.getpid(),os.getppid()))
# time.sleep(4)
# print('%s is done.Parent is is %s' % (os.getpid(),os.getppid()))
# # windows操作系统一定要在main下执行Process
# if __name__ == '__main__':
# p1 = Process(target=task) #实例化一个对象
# p2 = Process(target=task) #实例化一个对象
# p1.start() #仅仅是给操作系统发送了一个信号
# p2.start()
# p1.join()
# p2.join()
# print('主进程结束',os.getpid(),os.getppid())
# print(p1.pid)
########################################################################
# from multiprocessing import Process
# import time
# import os
# def task(name, n):
# print('%s is running' % name)
# time.sleep(n)
#
# # windows操作系统一定要在main下执行Process
# if __name__ == '__main__':
# start = time.time()
# p1 = Process(target=task, args=('子进程1',1)) #实例化一个对象
# p2 = Process(target=task, args=('子进程2',3)) #实例化一个对象
# p3 = Process(target=task, args=('子进程3',5))
# p1.start() #仅仅是给操作系统发送了一个信号,是由操作系统去调度是否启动线程,执行顺序是无法确定的!
# p2.start()
# p3.start()
# p1.join()
# p2.join()
# p3.join()
# print('主进程结束',(time.time()-start))
########################################################################
from multiprocessing import Process
import time
import os
def task(name, n):
print('%s is running' % name)
time.sleep(n) # windows操作系统一定要在main下执行Process
if __name__ == '__main__':
start = time.time()
p1 = Process(target=task, args=('子进程1',1), name='panda') #实例化一个对象
p2 = Process(target=task, args=('子进程2',3)) #实例化一个对象
p3 = Process(target=task, args=('子进程3',5))
p1.start() #仅仅是给操作系统发送了一个信号,是由操作系统去调度是否启动线程,执行顺序是无法确定的!
p1.join()
print(p2.is_alive())
p2.start()
print(p2.is_alive())
p2.join()
p3.start()
p3.join()
print('主进程结束',(time.time()-start))
print(p1.name)
Process的其他属性和方法
# from multiprocessing import Process
# import time
#
#
# def task(name):
# print('%s in running' % name)
# time.sleep(2)
#
# if __name__ == '__main__':
# p = Process(target=task, args=(('子进程1',)))
# # 守护进程的设置一定是要在子进程开始之前设置守护进程
# p.daemon = True
# p.start()
#
# print('主进程结束')
#---------------------------------------------------
# 练习题
from multiprocessing import Process
import time def foo():
print('')
time.sleep(1)
print('end123') def bar():
print('')
time.sleep(3)
print('end456') if __name__ == '__main__':
p1 = Process(target=foo)
p2 = Process(target=bar)
p1.daemon = True
p1.start()
p2.start()
time.sleep(0.5)
print('主进程结束')
守护进程
# 抢票的环节
# 1.先去看网站的票数
# 2.假设票数如果为0就不能再买票了
import time
import json
from multiprocessing import Process, Lock def search(name):
time.sleep(1) #模拟网络延迟
dic = json.load(open('db.txt', 'r', encoding='utf-8'))
print('<%s> 查看剩余的票数[%s]' % (name, dic['count'])) def get(name):
time.sleep(1)
dic = json.load(open('db.txt', 'r', encoding='utf-8'))
if dic['count'] > 0:
dic['count'] -= 1
time.sleep(3)
json.dump(dic, open('db.txt', 'w', encoding='utf-8'))
print('<%s>购票成功!' % name) def task(name, mutex):
search(name)
mutex.acquire()
get(name)
mutex.release() if __name__ == '__main__':
mutex = Lock()
for i in range(10):
p = Process(target=task, args=('路人%s' % i, mutex))
p.start()
模拟抢票
# 抢票的环节
# 1.先去看网站的票数
# 2.假设票数如果为0就不能再买票了
import time
import json
from multiprocessing import Process, Lock def search(name):
time.sleep(1) #模拟网络延迟
dic = json.load(open('db.txt', 'r', encoding='utf-8'))
print('<%s> 查看剩余的票数[%s]' % (name, dic['count'])) def get(name):
time.sleep(1)
dic = json.load(open('db.txt', 'r', encoding='utf-8'))
if dic['count'] > 0:
dic['count'] -= 1
time.sleep(3)
json.dump(dic, open('db.txt', 'w', encoding='utf-8'))
print('<%s>购票成功!' % name)
else:
print('<%s>购票失败!'%name) def task(name, ):
search(name)
# mutex.acquire()
get(name)
# mutex.release() if __name__ == '__main__':
# mutex = Lock()
for i in range(10):
p = Process(target=task, args=('路人%s' % i, ))
p.start()
p.join()
互斥锁与join的区别
from multiprocessing import Queue
# 队列里不应该放大的文件数据,一般发送的只是精简的小数据
# 队列确实可以不指定大小,但是队列的数据受限于内存的大小
# 队列会自动加锁
# 队列是多个进程之间通信使用的
q = Queue(3)
q.put('hello')
q.put({'a':1})
q.put([1,1,1,1])
# 查看队列是否满了
# print(q.full())
# 先进先出
print(q.get())
print(q.get())
print(q.get())
# print(q.empty())
队列的使用
from multiprocessing import Queue
# 队列里不应该放大的文件数据,一般发送的只是精简的小数据
# 队列确实可以不指定大小,但是队列的数据受限于内存的大小
# 队列会自动加锁
# 队列是多个进程之间通信使用的
q = Queue(3)
q.put('hello')
q.put({'a':1})
q.put([1,1,1,1])
# 查看队列是否满了
# print(q.full())
# 先进先出
print(q.get())
print(q.get())
print(q.get())
# print(q.empty())
查看pid(进程编号)
from multiprocessing import Process,Queue
import time
# 生产者在消费者消费的同时不能生产,消费者在生产的同时不能消费
# 这样的问题会导致生产者和消费者之间彼此在互相等待
# 生产者消费者模型就是可以解决这样的问题,
# 在生产者和消费者之间建立一个容器,生产者往容器里生产,消费者去容器里取,这样就解决了耦合问题
# 好处1:解耦合
# 好处2:平衡了数据处理的时间差
# 基于队列实现生产者消费者模型 def producer(q):
for i in range(10):
res = '制造品%s' % i
time.sleep(0.5)
print('生产者生产了%s' % res)
q.put(res) def consumer(q):
while True:
res = q.get()
if res is None: break
time.sleep(1)
print('消费者消费了%s'% res) if __name__ == '__main__':
# 启动队列->容器
q = Queue()
# 生产者
p = Process(target=producer, args=(q, )) # 消费者
c = Process(target=consumer, args=(q, )) p.start()
c.start()
p.join()
# 确保所有的生产者都生产完毕后才会给消费者发送结束信号(有几个消费者就要发几个结束信号)
q.put(None)
print('主进程')
生产者消费者模型
from multiprocessing import Process,JoinableQueue
import time
# 生产者在消费者消费的同时不能生产,消费者在生产的同时不能消费
# 这样的问题会导致生产者和消费者之间彼此在互相等待
# 生产者消费者模型就是可以解决这样的问题,
# 在生产者和消费者之间建立一个容器,生产者往容器里生产,消费者去容器里取,这样就解决了耦合问题
# 好处1:解耦合
# 好处2:平衡了数据处理的时间差
# 基于队列实现生产者消费者模型
# 回顾守护进程 def producer(q):
for i in range(2):
res = '制造品%s' % i
time.sleep(0.5)
print('生产者生产了%s' % res)
q.put(res)
q.join() def consumer(q):
while True:
res = q.get()
if res is None: break
time.sleep(1)
print('消费者消费了%s'% res)
q.task_done() if __name__ == '__main__':
# 启动队列->容器,JoinableQueue可以执行Queue.join
q = JoinableQueue()
# 生产者
p = Process(target=producer, args=(q, )) # 消费者
c = Process(target=consumer, args=(q, ))
c.daemon = True
p.start()
c.start()
p.join()
print('主进程')
joinableQueue
# 方式一:
# import time
# import random
# from threading import Thread
#
#
# def piao(name):
# print('%s piaoing' % name)
# time.sleep(random.randrange(1,5))
#
# if __name__ == '__main__':
# t1 = Thread(target=piao, args=('panda',))
# t1.start()
# # 站在执行的角度,这个就是主线程,站在资源的角度就是主进程
# print('主线程')
# --------------------------------------------------------------------
# 方式二:
# import time
# import random
# from threading import Thread
#
# class MyThread(Thread):
# def __init__(self, name):
# super().__init__()
# self.name = name
#
#
# def run(self):
# print('%s is running' % self.name)
# time.sleep(random.randrange(1,5))
# print('%s is ending' % self.name)
#
# if __name__ == '__main__':
# t1 = MyThread('panda')
# t1.start()
# print('主线程')
开启线程的两种方式
# 1.开进程的开销远大于开线程的开销
# 进程在启动之后由于要操作系统给进程分配内存空间,所以执行的时间上没有线程那样快速
# import time
# from threading import Thread
#
#
# def piao(name):
# print('%s piaoing' % name)
# time.sleep(2)
#
# if __name__ == '__main__':
# t1 = Thread(target=piao, args=('panda',))
# t1.start()
# # 站在执行的角度,这个就是主线程,站在资源的角度就是主进程
# print('主线程')
# --------------------------------------------------------------------
# 2.同一个进程内的多个线程共享该进程的地址内存空间 # from threading import Thread
# from multiprocessing import Process
# n = 100
# def task():
# global n
# n = 0
#
#
# if __name__ == '__main__':
# # p1 = Process(target=task, )
# # p1.start()
# # p1.join()
# t1 = Thread(target=task, )
# t1.start()
# t1.join()
# print('主线程', n)
# --------------------------------------------------------------------
# 3.pid->进程的id号
# 每开一个进程就会给进程分配id号
# from threading import Thread
# from multiprocessing import Process,current_process
# import os
# def task():
# print('线程%s' % os.getpid())
#
#
# if __name__ == '__main__':
# # p1 = Process(target=task, )
# # p1.start()
# # p1.join()
# t1 = Thread(target=task, )
# t1.start()
# t1.join()
# print('主', os.getpid())
进程和线程的区别
from threading import Thread,currentThread,active_count,enumerate
import time
def task():
print('%s is running' %currentThread().getName())
time.sleep(2)
print('%s is done' % currentThread().getName()) if __name__ == '__main__':
t = Thread(target=task, name='子线程-1')
t.start()
# 设置线程名称
# t.setName('子线程---1')
# t.join()
# 查看线程是否存活
# print(t.is_alive())
# print('主线程的名称',currentThread().getName())
# t.join()
# 查看当前执行线程活跃数
# print(active_count())
# 显示列表格式的,把当前活跃的线程对象放入列表里
print(enumerate())
Thread对象的其他属性和方法
# from threading import Thread
# import time
# def sayhi(name):
# time.sleep(2)
# print('%s say hello' % name)
#
# if __name__ == '__main__':
# t = Thread(target=sayhi, args=('panda',))
# t.setDaemon(True)
# t.start()
# print('主线程')
# print(t.is_alive())
# ----------------------------------------------------------------------
from threading import Thread
import time def foo():
print('start-123')
time.sleep(1)
print('end-123') def bar():
print('start-456')
time.sleep(3)
print('end-456') if __name__ == '__main__':
t1 = Thread(target=foo)
t2 = Thread(target=bar)
t1.daemon = True
t1.start()
t2.start()
print('main-----------')
守护线程
#mutex
from threading import Thread,Lock
import time
n = 100
def task():
global n
mutex.acquire()
temp = n
time.sleep(0.1)
n = temp-1
mutex.release() if __name__ == '__main__':
mutex = Lock()
t_l = []
for i in range(100):
t = Thread(target=task)
t_l.append(t)
t.start()
for t in t_l:
t.join()
print('主',n)
互斥锁
# from multiprocessing import Process
# from threading import Thread
# import os,time
# def work():
# res = 0
# for i in range(10000000):
# res *= i
# if __name__ == '__main__':
# l = []
# print(os.cpu_count())
# start = time.time()
# for i in range(4):
# # p = Process(target=work)
# p = Thread(target=work)
# l.append(p)
# p.start()
# for p in l:
# p.join()
# stop = time.time()
# print('spend time %s'%(stop-start))
# -----------------------------------------------
from multiprocessing import Process
from threading import Thread
import threading
import os,time
def work():
time.sleep(2)
print('===>') if __name__ == '__main__':
l=[]
print(os.cpu_count()) #本机为4核
start=time.time()
for i in range(400):
p=Process(target=work) #耗时12s多,大部分时间耗费在创建进程上
# p=Thread(target=work) #耗时2s多
l.append(p)
p.start()
for p in l:
p.join()
stop=time.time()
print('run time is %s' %(stop-start))
性能测试之GIL解释器
# from threading import Thread,Lock
# import time
# mutexA = Lock()
# mutexB = Lock()
#
# class MyThread(Thread):
# def run(self):
# self.f1()
# self.f2()
# def f1(self):
# mutexA.acquire()
# print('%s 拿到了A'% self.name)
# mutexB.acquire()
# print('%s 拿到了B' % self.name)
# mutexB.release()
# mutexA.release()
# def f2(self):
# mutexB.acquire()
# print('%s 拿到了B' % self.name)
# time.sleep(0.1)
# mutexA.acquire()
# print('%s 拿到了A'%self.name)
# mutexA.release()
# mutexB.release()
#
#
# if __name__ == '__main__':
# for i in range(10):
# t = MyThread()
# t.start()
# ------------------------------------------------------------
# 递归锁:可以连续acquire多次,每一次acquire一次,计数器就+1,只要计数为0,才能被其他线程抢到
# from threading import Thread,RLock
# import time
# mutexB = mutexA = RLock()
#
# class MyThread(Thread):
# def run(self):
# self.f1()
# self.f2()
# def f1(self):
# mutexA.acquire()
# print('%s 拿到了A'% self.name)
# mutexB.acquire()
# print('%s 拿到了B' % self.name)
# mutexB.release()
# mutexA.release()
# def f2(self):
# mutexB.acquire()
# print('%s 拿到了B' % self.name)
# time.sleep(0.1)
# mutexA.acquire()
# print('%s 拿到了A'%self.name)
# mutexA.release()
# mutexB.release()
#
#
# if __name__ == '__main__':
# for i in range(10):
# t = MyThread()
# t.start()
死锁与递归锁
from threading import Thread,Semaphore,currentThread
import time
import random
sm = Semaphore(5) def task():
# print('%s in '%currentThread().getName())
with sm:
print('%s in ' % currentThread().getName())
time.sleep(random.randint(1,3)) if __name__ == '__main__':
for i in range(10):
t = Thread(target=task)
t.start()
信号量
from threading import Thread, Event
import time
event = Event()
# event.wait()
# event.set() def student(name):
print('学生%s 正在听课' % name)
event.wait(3)
print('学生%s 课间活动' % name) def teacher(name):
print('老师%s正在上课'%name)
time.sleep(7)
event.set() if __name__ == '__main__':
stu1 = Thread(target=student, args=('panda',))
stu2 = Thread(target=student,args=('boy',))
stu3 = Thread(target=student, args=('zombie',))
t1 = Thread(target=teacher,args=('girl',))
stu1.start()
stu2.start()
stu3.start()
t1.start()
Event事件
from threading import Timer def task(name):
print('hello %s' % name) t = Timer(5 , task , args=('panda',))
t.start()
#-----------------------------------------------------
import random
from threading import Timer class Code: def __init__(self):
self.make_cache() def make_cache(self, interval=3):
self.cache = self.make_code()
print(self.cache)
self.t = Timer(interval, self.make_cache)
self.t.start()
def make_code(self,n=4):
res = ''
for i in range(n):
s1 = str(random.randint(0,9))
s2 = chr(random.randint(65,90))
res += random.choice([s1,s2])
return res def check(self):
while True:
code = input('请输入验证码>>').strip()
if code.upper() == self.cache:
print('验证码输入正确')
self.t.cancel()
break
obj = Code()
obj.check()
定时器
import queue
# 先进先出->队列功能
q = queue.Queue(3)
q.put('first')
q.put(2)
q.put('third')
q.put(4, block=True, timeout=3)
# 放了多余的值就会因为锁的原因卡主
print(q.get())
print(q.get())
print(q.get())
线程queue
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from multiprocessing import Process
import os
import time
import random
# 线程池和进程池的接口是一样的,那么什么时候用进程池,什么时候用线程池
# 池本质就是开进程和线程,本质没有区别
# 计算密集型,需要用多核的优势时候就要用进程池
# I/O密集型,不需要过多CPU资源的时候就要用线程池 def task(name):
print('name is %s pid is %s'%(name, os.getpid()))
time.sleep(random.randint(1,3)) if __name__ == '__main__':
# 进程池最多装4个进程
# pool = ThreadPoolExecutor(4)
pool = ProcessPoolExecutor(4)
for i in range(10):
# 异步调用,提交完任务以后,不用等任务拿到结果,只负责任务做完
pool.submit(task,'panda %s'%i)
pool.shutdown()
print('主线程')
进程池和线程池
# 提交任务的两种方式
# 1.同步调用:提交完任务后,就等待任务执行完毕,拿到结果,再执行下一行代码
# 同步调用的结果就是串行执行的方式
# import time
# import random
# from concurrent.futures import ThreadPoolExecutor
#
#
# def la(name):
# print('%s is laing' % name)
# time.sleep(random.randint(1,3))
# res = random.randint(7,13)*"*"
# return {'name':name, 'res':res}
#
#
# def weight(obj):
# name = obj['name']
# size = obj['res']
# print('%s is %s'%(name, size))
#
#
# if __name__ == '__main__':
# pool = ThreadPoolExecutor(10)
# res1 = pool.submit(la, 'panda').result()
# weight(res1)
# res2 = pool.submit(la, 'zombie').result()
# weight(res2)
# res3 = pool.submit(la, 'boy').result()
# weight(res3)
# 2.异步调用:提交完任务以后不在原地等待任务执行完毕,
import time
import random
from concurrent.futures import ThreadPoolExecutor def la(name):
print('%s is laing' % name)
time.sleep(random.randint(1,3))
res = random.randint(7,13)*"*"
return {'name':name, 'res':res} def weight(obj):
obj = obj.result()
name = obj['name']
size = obj['res']
print('%s is %s'%(name, size)) if __name__ == '__main__':
pool = ThreadPoolExecutor(10)
pool.submit(la, 'panda').add_done_callback(weight)
pool.submit(la, 'zombie').add_done_callback(weight)
pool.submit(la, 'boy').add_done_callback(weight)
同步调用和异步调用
def producer():
g = consumer()
next(g)
for i in range(100000000):
g.send(i) def consumer():
while True:
res = yield producer()
协程
from greenlet import greenlet def eat(name):
print('%s eat 1' % name)
g2.switch('panda')
print('%s eat 2' % name)
g2.switch() def play(name):
print('%s play 1' % name)
g1.switch()
print('%s play 2' % name) g1 = greenlet(eat)
g2 = greenlet(play)
# 切换
g1.switch('panda')
greenlet模块
import gevent
import time
from gevent import monkey
# 打补丁,所有的设计IO操作的部分都全部打上补丁,做上标记
monkey.patch_all() def eat(name):
print('%s eat 1' % name)
time.sleep(3)
print('%s eat 2' % name) def play(name):
print('%s play 1' % name)
time.sleep(5)
print('%s play 2' % name) g1 = gevent.spawn(eat, 'panda')
g2 = gevent.spawn(play, 'boy')
g1.join()
g2.join()
gevent模块
import gevent
import time
from gevent import monkey;monkey.patch_all()
# 打补丁,所有的设计IO操作的部分都全部打上补丁,做上标记 def eat(name):
print('%s eat 1' % name)
time.sleep(3)
print('%s eat 2' % name) def play(name):
print('%s play 1' % name)
time.sleep(5)
print('%s play 2' % name) g1 = gevent.spawn(eat, 'panda')
g2 = gevent.spawn(play, 'boy')
gevent.joinall([g1,g2])
gevent异步提交
路飞学城Python-Day32【小结】的更多相关文章
- 路飞学城—Python爬虫实战密训班 第三章
路飞学城—Python爬虫实战密训班 第三章 一.scrapy-redis插件实现简单分布式爬虫 scrapy-redis插件用于将scrapy和redis结合实现简单分布式爬虫: - 定义调度器 - ...
- 路飞学城—Python爬虫实战密训班 第二章
路飞学城—Python爬虫实战密训班 第二章 一.Selenium基础 Selenium是一个第三方模块,可以完全模拟用户在浏览器上操作(相当于在浏览器上点点点). 1.安装 - pip instal ...
- 路飞学城Python爬虫课第一章笔记
前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 之前看阮一峰的博客文章,介绍到路飞学城爬虫课程限免,看了眼内容还不错,就兴冲冲报了名,99块钱满足以下条件会返还并送书送视频. 缴 ...
- 路飞学城-Python开发集训-第3章
学习心得: 通过这一章的作业,使我对正则表达式的使用直接提升了一个level,虽然作业完成的不怎么样,重复代码有点多,但是收获还是非常大的,有点找到写代码的感觉了,遗憾的是,这次作业交过,这次集训就结 ...
- 路飞学城-Python开发集训-第2章
学习心得: 这章对编码的讲解超级赞,现在对于编码终于有一点认知了,但还没有大彻大悟,还需要更加细心的琢磨一下Alex博客和视频,以前真的是被编码折磨死了,因为编码的问题而浪费的时间很多很多,现在终于感 ...
- 路飞学城-Python开发集训-第1章
学习体会: 在参加这次集训之前我自己学过一段时间的Python,看过老男孩的免费视频,自我感觉还行,老师写的代码基本上都能看懂,但是实际呢?....今天是集训第一次交作业的时间,突然发现看似简单升级需 ...
- 路飞学城-Python开发集训-第4章
学习心得: 学习笔记: 在python中一个py文件就是一个模块 模块好处: 1.提高可维护性 2.可重用 3.避免函数名和变量名冲突 模块分为三种: 1.内置标准模块(标准库),查看所有自带和第三方 ...
- 路飞学城-Python开发-第二章
''' 数据结构: menu = { '北京':{ '海淀':{ '五道口':{ 'soho':{}, '网易':{}, 'google':{} }, '中关村':{ '爱奇艺':{}, '汽车之家' ...
- 路飞学城-Python开发-第三章
# 数据结构: # goods = [ # {"name": "电脑", "price": 1999}, # {"name&quo ...
- 路飞学城-Python开发-第一章
# 基础需求: # 让用户输入用户名密码 # 认证成功后显示欢迎信息 # 输错三次后退出程序 username = 'pandaboy' password = ' def Login(username ...
随机推荐
- Camera三维动画
一.概述 在Android中说到3D开发,我们首先想到的是OpenGL,但用起来比较复杂繁琐,不适合做应用级别的3D变换.Android为我们提供了一个简化版的3D开发入口:Camera(这里的Cam ...
- Linux下mysql 忘记密码的解决办法
>mysql -u root -p Enter password: ******** Welcome to the MySQL monitor. Commands end with ; or \ ...
- 【XSY3350】svisor - 点分治+虚树dp
题目来源:NOI2019模拟测试赛(九) 题意: 吐槽: 第一眼看到题觉得这不是震波的完全弱化版吗……然后开开心心的码了个点分治 码到一半突然发现看错题了……心态崩了于是就弃疗手玩提答去了 于是就快乐 ...
- vue中数组变动更新检测
Vue 包含两种观察数组的方法分别如下 1.变异方法 顾名思义,变异方法会改变被这些方法调用的原始数组,它们也将会触发视图更新,这些方法如下 push() pop() shift() unshift( ...
- 洛谷P1046 陶陶摘苹果
题目描述 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出 101010 个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个 303030 厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳 ...
- javax.servlet.http.HttpServletRequest; 不存在
右击项目 找到 最后一项 属性设置 选择 Server Runtime 选择导入你的 tomcat jar 包
- Problem 6
Problem 6 # Problem_6.py """ The sum of the squares of the first ten natural numbers ...
- GA求解TSP
遗传算法中包含了如下5个基本要素: (1)对参数进行编码: (2)设定初始种群大小: (3)适应度函数的设计: (4)遗传操作设计: (5)控制参数设定(包括种群大小.最大进化代数.交叉率.变异率等) ...
- 《UML精粹》第三章 -类图的基本概念
第三章 类图:基本概念 类图可用来描写叙述系统中各种对象的类型.也可描绘出对象间各种各样的静态关系.此外.类图中也能够秀出类的性质(property)与操作(operation),以及可应用到对象间连 ...
- cocos2dx项目创建
射击类游戏文档 作者:浙江传媒学院 新媒体 张勇 1>编译环境 首先我们先去cocos2dx官网上下载cocos2dx最新版本号 http://www.cocos2d-x.org/ 我下载的 ...