日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發(fā)布時間:2023/12/9 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python异步处理请求_如何一次在python中发送异步http请求? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1)創(chuàng)建一個Queue.Queue對象

2)根據(jù)您的喜好制作盡可能多的“工作”線程,從Queue.Queue讀取

3)將作業(yè)提供給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,創(chuàng)建對象然后在實例上調(diào)用“start”

總結(jié)

以上是生活随笔為你收集整理的python异步处理请求_如何一次在python中发送异步http请求?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。