Im facing difficulties while trying to execute around 10 functions simultaneously using asyncio in Django. I could not find nay clear documentation on how to use asyncio with django.
I made around 10 http requests to different using TOR which is by default slow. Instead of making these requests one by one, which usually takes around 2 minutes, I wanted to perform all the 10 requests simultaneously. There are 10 distinct functions each making a http request a different URL and scrape data and return JSON.
URLscrape.py :
async def scrape1(username):
response = request.get('http://example.com'+username)
return response.json()
async def scrape2(username):
response = request.get('http://something.com'+username)
return response.json()
I have 10 individual functions like the above with different URLs and perfrom scraping and return a json data. In Django views, I did like this:
Views.py
from URLscrape import scrape1, scrape2........scrape10
def scrapper():
loop = asyncio.get_event_loop()
feature1 = loop.run_in_executor(None, scrape1, username)
feature2 = loop.run_in_executor(None, scrape2, username)
....
feature10 = loop.run_in_executor(None, scrape10, username)
response1 = await future1
response2 = await future2
.....
response10 = await future10
response1 = response1.text
......
response10 = response2.text
return render(request, 'index.html', {'scrape1':response1,'scrape10':response10})
But I dont know how to use loop.run_until_complete() to complete the script. Im in a restricted situation to use 10 individual functions for scraping. Im want to run the 10 simultaneously but I dont know how. I could not understand the concept and syntax of asyncio. Please help!!!