How to use python multiprocessing module in django view

前端 未结 3 883
醉梦人生
醉梦人生 2021-01-02 22:24

I have a simple function that go over a list of URLs, using GET to retrieve some information and update the DB (PostgresSQL) accordingly. The funct

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-02 23:11

    Currently I have a function (view) that go over each URL to get the information, and update the DB.

    It means response time does not matter for you and instead of doing it in the background (asynchronously), you are OK with doing it in the foreground if your response time is cut by 4 (using 4 sub-processes/threads). If that is the case you can simply put your sample code in your view. Like

    from multiprocessing import Pool
    
    def updateDB(ip):
         code goes here...
    
    def my_view(request):
        pool = Pool(processes=4)              # process per core
        pool.map(updateDB, ip)
        return HttpResponse("SUCCESS")
    

    But, if you want to do it asynchronously in the background then you should use Celery or follow one of @BasicWolf's suggestions.

提交回复
热议问题