Python学习笔记--gevent嵌套使用
这篇主要是接着上篇的,实验gevent嵌套使用,看情况如何。还是先上代码。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2020-03-02 19:53:11
# @Author : Flyinghappy (671474@qq.com)
# @Link : https://www.cnblogs.com/flyinghappy/
# @Version : $Id$
#note:gevent嵌套使用实验
from gevent import monkey
monkey.patch_all()
import gevent
import time
import asyncio
import requests
import urllib.request
def runinfo(func):
def inner(*args):
print('开始访问---'+str(args[0]))
start_time=time.time()
result=func(*args)
stop_time=time.time()
print(func.__name__+'------running time is: %s'% (stop_time-start_time))
print('结束访问---'+str(args[0]))
return result
return inner
@runinfo
def taskfun(url):
html=urllib.request.urlopen(url).read()
return html
@runinfo
def taskfun_outer(num_list):
url=[
'http://www.sina.com.cn',
'http://www.cnr.cn',
'http://www.hao123.com'
] g_list=[]
for i in range(len(url)):
g=gevent.spawn(taskfun,url[i])
g_list.append(g)
gevent.joinall(g_list) def main():
start_time=time.time()
num_list=['一']
g1=gevent.spawn(taskfun_outer,num_list)
num_list=['二']
g2=gevent.spawn(taskfun_outer,num_list)
gevent.joinall([g1,g2])
stop_time=time.time()
print('main---running time is: %s'% (stop_time-start_time))
if __name__ == '__main__':
main()
再来看看运行结果哈:我截图了两张,大家可以仔细对比一下。说明是异步执行的。

另外一张截图

PS:呵呵呵,说明实验成功了!可以嵌套使用。
Python学习笔记--gevent嵌套使用的更多相关文章
- Python学习笔记(九)
Python学习笔记(九): 装饰器(函数) 内置函数 1. 装饰器 1. 作用域 2. 高阶函数 3. 闭包 如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就 ...
- Python学习笔记(七)
Python学习笔记(七): 深浅拷贝 Set-集合 函数 1. 深浅拷贝 1. 浅拷贝-多层嵌套只拷贝第一层 a = [[1,2],3,4] b = a.copy() print(b) # 结果:[ ...
- python学习笔记(二)、字符串操作
该一系列python学习笔记都是根据<Python基础教程(第3版)>内容所记录整理的 1.字符串基本操作 所有标准序列操作(索引.切片.乘法.成员资格检查.长度.最小值和最大值)都适用于 ...
- Python学习笔记010——匿名函数lambda
1 语法 my_lambda = lambda arg1, arg2 : arg1 + arg2 + 1 arg1.arg2:参数 arg1 + arg2 + 1 :表达式 2 描述 匿名函数不需要r ...
- Python学习笔记(四)函数式编程
高阶函数(Higher-order function) Input: 1 abs Output: 1 <function abs> Input: 1 abs(-10) Output: 1 ...
- Python 学习笔记(下)
Python 学习笔记(下) 这份笔记是我在系统地学习python时记录的,它不能算是一份完整的参考,但里面大都是我觉得比较重要的地方. 目录 Python 学习笔记(下) 函数设计与使用 形参与实参 ...
- Python 学习笔记(上)
Python 学习笔记(上) 这份笔记是我在系统地学习python时记录的,它不能算是一份完整的参考,但里面大都是我觉得比较重要的地方. 目录 Python 学习笔记(上) 基础知识 基本输入输出 模 ...
- Python学习笔记.md
Python学习笔记 1.变量类型 x=5 int x="ss" string x='a' string x=True bool #查看变量类型 type(x) 2.字符串常用操作 ...
- python学习笔记整理——字典
python学习笔记整理 数据结构--字典 无序的 {键:值} 对集合 用于查询的方法 len(d) Return the number of items in the dictionary d. 返 ...
随机推荐
- 指定linux 下默认python版本
sudo rm -rf python sudo ln -s /usr/bin/python3 /usr/bin/python
- 利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别
用已经搭建好 face_recognition,dlib 环境来进行人脸识别 未搭建好环境请参考:https://www.cnblogs.com/guihua-pingting/p/12201077. ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- mybatis处理LIKE模糊查询字符串拼接
-- 最佳实践 <select id="getSealByMap" parameterType="map" resultType="map&qu ...
- java静态方法和静态字段
public class Dog{ public static void main(String[]args){ A a= new A(); a.add(); //java实例对象可以访问类的静态方法 ...
- VMware 安装 Centos7 后,没有ipv4的地址,或者地址显示127.0.0.1
问题描述:VMware 安装 Centos7 后,没有ipv4的地址,或者地址显示127.0.0.1 安装环境:VMware版本:VMware-workstation-full-14.0.0.2405 ...
- 在Python 中怎么表示一个元素在一个list中的数量?
commonest = [1,2,2,2,1,3,4,5,1,1] print(commonest.count(1))
- git实用手册
git.exe clone --progress -v "git@code.sohuno.com:huimingtao/focus-wap.git" "D:\worksp ...
- iOS动画效果集合、 通过摄像头获取心率、仿淘宝滑动样式、瀑布流、分类切换布局等源码
iOS精选源码 动画知识运用及常见动画效果收集 较为美观的多级展开列表 MUImageCache -简单轻量的图片缓存方案 iOS 瀑布流之栅格布局 一用就上瘾的JXCategoryView iOS ...
- Hadoop_ssh免密认证出现的问题
问题 network is unreachable 网络解决方法 看了大部分文章博客,发现都是说网关或者没ping好,我都试了,发现没用 我的解决方法 把虚拟机右上角的网络连接即可(我的是S开头的一个 ...