Home  >  Q&A  >  body text

Python speeds up program running problems

Assumption:

def get(url):
    #这里是get请求
    return url

for i in urls:
    get(i)
#这里采用循环去调用请求
#这样速度好像比较慢,有什么方法可以
#当多个请求时,也可以快速完成程序

Thank you

PHPzPHPz2711 days ago648

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-05-18 10:47:07

    >>> from eventor import Eventor
    >>> urllist = ["https://a.com", ....]
    >>> def get(url):
            # 网络IO耗时
            return url
    >>> e = Eventor(threadcount=3, taskunitcount=3, func=get, interval=1)
    >>> result = e.run_with_tasklist(urllist)
    >>> print(result)
    

    Choose multi-core, multi-process or multi-thread according to your needs, coroutines speed up processing.

    Move details: https://github.com/kute/eventor

    We will add async await, celery worker, etc. in the future

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-18 10:47:07

    Several methods and can be used in combination

    1. coroutine

    2. multiprocess

    3. multithread

    4. celery worker

    reply
    0
  • PHP中文网

    PHP中文网2017-05-18 10:47:07

    The simplest modification is to use multiprocessing.dummy to perform multi-threaded tasks. You can check it, set the number of threads, and then it can be used without much modification to the program.

    reply
    0
  • Cancelreply