简体   繁体   English

限制多处理python中的进程数

[英]Limiting number of processes in multiprocessing python

My requirement is to generate hundreds of HTTP POST requests per second . 我的要求是hundreds of HTTP POST requests per second生成hundreds of HTTP POST requests per second I am doing it using urllib2 . 我正在使用urllib2

def send():
    req = urllib2.Request(url)
    req.add_data(data)
    response = urllib2.urlopen(req)

while datetime.datetime.now() <= ftime:
    p=Process(target=send, args=[])
    p.start()
    time.sleep(0.001)

The problem is this code sometimes for some iterations throws either of following exceptions: 问题在于此代码sometimes for some iterations抛出以下异常之一:

HTTP 503 Service Unavailable.
URLError: <urlopen error [Errno -2] Name or service not known>

I have tried using requests(HTTP for humans) as well but I am having some proxy issues with that module. 我也尝试过使用requests(HTTP for humans)使用requests(HTTP for humans) ,但是该模块存在一些代理问题。 Seems like requests is sending http packets to proxy server even when target machine is within same LAN. 似乎即使目标计算机在同一局域网内, requests正在向代理服务器发送http数据包。 I don't want packets to go to proxy server. 我不希望数据包进入代理服务器。

The simplest way to limit number of concurrent connections is to use a thread pool: 限制并发连接数的最简单方法是使用线程池:

#!/usr/bin/env python
from itertools import izip, repeat
from multiprocessing.dummy import Pool # use threads for I/O bound tasks
from urllib2 import urlopen

def fetch(url_data):
    try:
        return url_data[0], urlopen(*url_data).read(), None
    except EnvironmentError as e:
        return url_data[0], None, str(e)

if __name__=="__main__":
    pool = Pool(20) # use 20 concurrent connections
    params = izip(urls, repeat(data)) # use the same data for all urls
    for url, content, error in pool.imap_unorderred(fetch, params):
        if error is None:
           print("done: %s: %d" % (url, len(content)))
        else:
           print("error: %s: %s" % (url, error))

503 Service Unavailable is a server error. 503 Service Unavailable是服务器错误。 It might fail to handle the load. 它可能无法处理负载。

Name or service not known is a dns error. Name or service not known是dns错误。 If you need make many requests; 如果您需要提出许多要求; install/enable a local caching dns server. 安装/启用本地缓存dns服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM