python-多进程协程压垮服务器

2017-01-01 10:46

代码如下:

from multiprocessing import Process
from gevent import monkey; monkey.patch_all()
import gevent
import requests
url = "XXX"
class Producer(object):
    def __init__(self):
       self._rungevent()
    def _rungevent(self):
        jobs = []
        for i in range(1000): #windows下有1024端口限制
            jobs.append(gevent.spawn(self.produce))
        gevent.joinall(jobs)
    def produce(self):
        # params = urllib.parse.urlencode(params)
        r = requests.get(url)
        if r.status_code == 200:
            r.encoding = 'UTF-8'
            print(r.text)
            return r.text
        else:
            print("失败咯")
        return {}
def main():
    p = Process(target = Producer, args=())  #一个进程启动协程
    p.start()

    p1 = Process(target = Producer, args=())
    p1.start()


if __name__ == '__main__':
    main()
  • 看看服务器

Paste_Image.png