python请求url非阻塞_如何在python中执行非阻塞URL获取

下面是一个如何使用threading.thread的示例。只需用自己的类名替换类名,用自己的函数替换run函数。请注意,线程对于像您这样的限制IO的应用程序是非常好的,并且可以真正加快速度。在标准的python中严格使用pythong线程进行计算没有帮助,因为一次只能计算一个线程。

import threading, time

class Ping(threading.Thread):

def __init__(self, multiple):

threading.Thread.__init__(self)

self.multiple = multiple

def run(self):

#sleeps 3 seconds then prints 'pong' x times

time.sleep(3)

printString = 'pong' * self.multiple

pingInstance = Ping(3)

pingInstance.start() #your run function will be called with the start function

print "pingInstance is alive? : %d" % pingInstance.isAlive() #will return True, or 1

print "Number of threads alive: %d" % threading.activeCount()

#main thread + class instance

time.sleep(3.5)

print "Number of threads alive: %d" % threading.activeCount()

print "pingInstance is alive?: %d" % pingInstance.isAlive()

#isAlive returns false when your thread reaches the end of it's run function.

#only main thread now

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值