python使用tomorrow实现多线程的例子

yipeiwu_com6年前Python基础

如下所示:

import time,requestes
from tomorrow import threads
@threads(10)#使用装饰器,这个函数异步执行
def  download(url):

  return requests.get(url)
def  main():
  start = time.time()
  urls = [
   'https://pypi.org/project/tomorrow/0.2.0/',
   'https://www.cnblogs.com/pyld/p/4716744.html',
   'http://www.xicidaili.com/nn/10',
   'http://baidu.com',
   'http://www.bubuko.com/infodetail-1028793.html?yyue=a21bo.50862.201879',
  ]
  responses = [download(i) for i in urls]
  end = time.time()
  print("Time: %f seconds" % (end - start))
if  __name__=="__main__":
  main()

不使用多线程:

使用多线程:

以上这篇python使用tomorrow实现多线程的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Django CBV类的用法详解

前言 之前我们在路由匹配的时候,一个url对应一个函数,其实我们还可以一个url对应一个类,这个就是CBV,下面我们来简单的介绍一下CBV 一、CBV的基本用法 1、在路由匹配中要这样...

Python 实现异步调用函数的示例讲解

async_call.py #coding:utf-8 from threading import Thread def async_call(fn): def wrapper...

基于Python的PIL库学习详解

摘要 对于图像识别,大量的工作在于图像的处理,处理效果好,那么才能很好地识别,因此,良好的图像处理是识别的基础。在Python中,有一个优秀的图像处理框架,就是PIL库,本博文会分模块...

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)

python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换) Python3 JSON 数据解析...

python读写Excel表格的实例代码(简单实用)

python读写Excel表格的实例代码(简单实用)

安装两个库:pip install xlrd、pip install xlwt 1.python读excel——xlrd 2.python写excel——xlwt 1.读excel数据,...