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设计】。

相关文章

把csv文件转化为数组及数组的切片方法

在Python中我们经常会用到两个库Numpy和pandas csv文件转化为数组 import numpy my_matrix = numpy.loadtxt(open("c:\\...

解读Django框架中的低层次缓存API

有些时候,对整个经解析的页面进行缓存并不会给你带来太多好处,事实上可能会过犹不及。 比如说,也许你的站点所包含的一个视图依赖几个费时的查询,每隔一段时间结果就会发生变化。 在这种情况下,...

浅谈python的输入输出,注释,基本数据类型

1.输入与输出 python中输入与输出函数为:print、input help() 帮助的使用:help() help(print) print(value, ..., sep=...

django中send_mail功能实现详解

前言 我们大家应该都知道python中smtplib模块用于邮件的功能,而django对这个这模块进行封装,使得它使用起来十分简单。 django.core.mail就是django邮件...

Python的迭代器和生成器

先说迭代器,对于string、list、dict、tuple等这类容器对象,使用for循环遍历是很方便的。在后台for语句对容器对象调用iter()函数,iter()是python的内置...