today is another question day. I am doing requests in python, and I am trying to speed things up. if you recall, I already asked a question about this. Today i have finished that code, and move on to face a similar problem.
Running this code produces a RuntimeError
. It says:
RuntimeError: There is no current event loop in thread 'Thread-2'
I don't know why I am getting this error, since I am using threading.Thread() instead of asyncio. Why is it asking for an event loop?
def func(mininum, maximum=None):
if maximum == None:
maximum = mininum
import asyncio
import aiohttp
from bs4 import BeautifulSoup
async def find_account(i_d, session):
try:
async with session.get(f'https://web.roblox.com/users/{i_d}/profile') as response:
if response.status == 200:
r = await response.read()
soup = BeautifulSoup(r, 'html.parser')
stuff = list(soup.find_all('h2'))
stuff = stuff[0]
stuff = list(stuff)
stuff = stuff[0]
print(f'{i_d} is done')
return str(stuff) + ' ID: {id}'.format(id=i_d)
else:
return 'None'
except aiohttp.ServerDisconnectedError:
await find_account(i_d, session)
except asyncio.exceptions.TimeoutError:
await find_account(i_d, session)
async def id_range(minimum, maximum):
tasks = []
async with aiohttp.ClientSession() as session:
for i in range(minimum, maximum + 1):
tasks.append(asyncio.create_task(find_account(i_d=i, session=session)))
return await asyncio.gather(*tasks)
event_loop = asyncio.get_event_loop()
return event_loop.run_until_complete(id_range(mininum, maximum))
import threading
all = []
p1 = threading.Thread(target=func, args=(1,1000)).start()
p2 = threading.Thread(target=func, args=(1001,2000)).start()
p1 : threading.Thread
p2 : threading.Thread
p1.join()
p2.join()```
#Comment if you need more traceback.
You cannot use the async
/ await
keywords when using threading. Instead, the OS will decide automatically when to switch threads.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.