通过线程来实现多任务并发。提高性能。先看看例子。

 #!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2020-03-02 21:10:39
# @Author : Flyinghappy (671474@qq.com)
# @Link : https://www.cnblogs.com/flyinghappy/
# @Version : $Id$
import time
import threading
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',
'http://www.taobao.com',
'https://www.eastmoney.com'
]
thread_list=[]
for i in range(len(url)):
t=threading.Thread(target=taskfun,args=(url[i],))
thread_list.append(t)
for item in thread_list:
item.start()
for item in thread_list:
item.join() def main():
start_time=time.time()
num_list=['taskfun_outer']
taskfun_outer(num_list)
stop_time=time.time()
print('main---running time is: %s'% (stop_time-start_time))
if __name__ == '__main__':
main()

测试结果:从用的时间看访问的任务是并发的,还可以从执行的顺序来看。

最新文章

  1. 数据库中的two phase locking
  2. 深入理解javascript原型和闭包(10)——this
  3. 谈事件冒泡(Bubble)和事件捕捉(capture)
  4. 【代码笔记】iOS-scrollerView里多个tableView加搜索框
  5. inline-block 前世今生
  6. 局域网内sqldeveloper客户端连接oracle服务器
  7. sublime text帮你更好的写python
  8. 分享一个在线生成微信跳转链接实现微信内跳转浏览器打开URL的工具
  9. [django]date类型和datetime类型过滤
  10. Cracking The Coding Interview 9.7
  11. .Net Discovery系列之十一-深入理解平台机制与性能影响 (中)
  12. chapter1 初识Go语言
  13. OpenCV学习(30) 轮廓defects
  14. BurpSuite 激活破解
  15. 【C++】new和delete表达式与内存管理
  16. Linux SCIM/fcitx/ibus 输入法
  17. hadoop MapReduce辅助排序解析
  18. python实战===itchat
  19. 关于springboot 打包问题 jar包和 war包
  20. windows下 安装python_ldap MySQL-python

热门文章

  1. RHEL7在线安装rvm(ruby管理包)
  2. springboot学习笔记:12.解决springboot打成可执行jar在linux上启动慢的问题
  3. python学习笔记(18)字典和json 的区别 和转换
  4. signal——信号集
  5. fcntl()函数之非阻塞模型
  6. IDEA Maven项目中添加tomcat没有无artifact选项
  7. [LC] 362. Design Hit Counter
  8. stress命令安装
  9. leetcode第22题:括号生成
  10. Python之configparser配置文件的读取