python异步处理请求_如何一次在python中发送异步http请求?

1)创建一个Queue.Queue对象

2)根据您的喜好制作尽可能多的“工作”线程,从Queue.Queue读取

3)将作业提供给Queue.Queue

工作线程将按照它们放置的顺序读取Queue.Queue

从文件中读取行并将它们放入Queue.Queue的示例

import sys

import urllib2

import urllib

from Queue import Queue

import threading

import re

THEEND = "TERMINATION-NOW-THE-END"

#read from file into Queue.Queue asynchronously

class QueueFile(threading.Thread):

def run(self):

if not(isinstance(self.myq, Queue)):

print "Queue not set to a Queue"

sys.exit(1)

h = open(self.f, 'r')

for l in h:

self.myq.put(l.strip()) # this will block if the queue is full

self.myq.put(THEEND)

def set_queue(self, q):

self.myq = q

def set_file(self, f):

self.f = f

了解工作线程可能是什么样的(仅限示例)

class myWorker(threading.Thread):

def run(self):

while(running):

try:

data = self.q.get() # read from fifo

req = urllib2.Request("http://192.168.1.10/url/path")

req.add_data(urllib.urlencode(data))

h1 = urllib2.urlopen(req, timeout=10)

res = h1.read()

assert(len(res) > 80)

except urllib2.HTTPError, e:

print e

except urllib2.URLError, e:

print "done %d reqs " % n

print e

sys.exit()

要使对象基于threading.Thread go,创建对象然后在实例上调用“start”

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值