推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
pc10201
V2EX  ›  Python

为什么 python 多线程还要比 gevent 要快一点?

  •  
  •   pc10201 · Feb 19, 2014 · 6304 views
    This topic created in 4492 days ago, the information mentioned may be changed or developed.
    代码如下,初学python,如有不正确的地方请大神指正
    #coding=utf-8
    import requests
    import threading
    import time
    import Queue
    geturl='http://192.168.1.41/login.php'#内部服务器地址,可以改成自己的
    xrange_num=1000#设置循环总次数
    con_num=20#设置线程数
    start=time.time()
    class testThread(threading.Thread):
    def __init__(self,queue):
    threading.Thread.__init__(self)
    self.queue = queue

    def run(self):
    while 1:
    try:
    url= self.queue.get_nowait()
    except Queue.Empty:
    raise SystemExit
    requests.get(url)

    queue = Queue.Queue()
    for x in xrange(xrange_num):
    #line = line.strip()
    queue.put(geturl)
    threads = []

    for dummy in xrange(con_num):
    t = testThread(queue)
    t.start()
    threads.append(t)

    # Wait for all threads to finish
    for thread in threads:
    thread.join()

    print time.time()-start

    import gevent
    from gevent import monkey
    monkey.patch_all()
    from gevent.pool import Pool
    import requests

    start2=time.time()
    p = Pool(con_num)
    def down(url):
    requests.get(url)

    for x in xrange(xrange_num):
    p.spawn(down, geturl)

    p.join()
    print time.time()-start2
    raw_input()
    1 replies    2017-03-08 16:05:22 +08:00
    isofia
        1
    isofia  
       Mar 8, 2017
    竟然一个回复都没有
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   942 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 20:50 · PVG 04:50 · LAX 13:50 · JFK 16:50
    ♥ Do have faith in what you're doing.