Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
774 views
in Technique[技术] by (71.8m points)

python - Throttling requests with multiple proxies

I'm currently assigning random proxies to requests via a custom middleware. I'd like to key download throttling to the specific proxy that the request is using, but as far as I can tell, out of the box, this is only possible when tied to domains or IPs. I'm worried that implementing pooling logic in the proxy middleware would cause thread safety issues. Has anyone done this before? Any pointers would be appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

As recommended on the Scrapy mailing list, there is a special request meta variable that the Autothrottle middleware obeys, called download_slot - this allows for programmatic grouping/throttling of requests.

In my custom proxy middleware:

self.proxies = get_proxies() #list of proxies
proxy_address = random.choice(self.proxies)
request.meta['proxy'] = proxy_address
request.meta['download_slot'] = hash(proxy_address) % MAX_CONCURRENT_REQUESTS

I use the hash function as a cheap way to bucket the requests by an externally defined limit on requests.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...