Aiohttp, Asyncio: RuntimeError: Event loop is closed

2024/10/14 7:14:59

I have two scripts, scraper.py and db_control.py. In scraper.py I have something like this:

...
def scrape(category, field, pages, search, use_proxy, proxy_file):...loop = asyncio.get_event_loop()to_do = [ get_pages(url, params, conngen) for url in urls ]wait_coro = asyncio.wait(to_do)res, _ = loop.run_until_complete(wait_coro)...loop.close()return [ x.result() for x in res ]...

And in db_control.py:

from scraper import scrape
...
while new < 15:data = scrape(category, field, pages, search, use_proxy, proxy_file)...
...

Theoretically, scraper should be started unknown-times until enough of data have been obtained. But when new is not imidiatelly > 15 then this error occurs:

  File "/usr/lib/python3.4/asyncio/base_events.py", line 293, in run_until_complete
self._check_closed()File "/usr/lib/python3.4/asyncio/base_events.py", line 265, in _check_closed
raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

But scripts works just fine if I run scrape() only once. So I guess there is some problem with recreating loop = asyncio.get_event_loop(), I have tried this but nothing changed. How I can fix this? Of course those are just snippets of my code, if you think problem can be elsewhere, full code is available here.

Answer

Methods run_until_complete, run_forever, run_in_executor, create_task, call_at explicitly check the loop and throw exception if it's closed.

Quote from docs - BaseEvenLoop.close:

This is idempotent and irreversible


Unless you have some(good) reasons, you might simply omit the close line:

def scrape(category, field, pages, search, use_proxy, proxy_file):#...loop = asyncio.get_event_loop()to_do = [ get_pages(url, params, conngen) for url in urls ]wait_coro = asyncio.wait(to_do)res, _ = loop.run_until_complete(wait_coro)#...# loop.close()return [ x.result() for x in res ]

If you want to have each time a brand new loop, you have t create it manually and set as default:

def scrape(category, field, pages, search, use_proxy, proxy_file):#...loop = asyncio.new_event_loop()asyncio.set_event_loop(loop)    to_do = [ get_pages(url, params, conngen) for url in urls ]wait_coro = asyncio.wait(to_do)res, _ = loop.run_until_complete(wait_coro)#...return [ x.result() for x in res ]
https://en.xdnf.cn/q/69441.html

Related Q&A

Python for ios interpreter [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Python or Ruby Interpreter on iOS I just discovered this apps pypad and python for ios They have like an interpreter an ed…

Detect when multiprocessing queue is empty and closed

Lets say I have two processes: a reader and a writer. How does the writer detect when the reader has finished writing values?The multiprocessing module has a queue with a close method that seems custo…

imshow and histogram2d: cant get them to work

Im learning Python and this is my first question here. Ive read other topics related to the usage of imshow but didnt find anything useful. Sorry for my bad English.I have plotted a set of points here,…

3D Waterfall Plot with Colored Heights

Im trying to visualise a dataset in 3D which consists of a time series (along y) of x-z data, using Python and Matplotlib.Id like to create a plot like the one below (which was made in Python: http://a…

python xlsxwriter: Keep header in excel when adding a table

I have a panda dataframe that I write to a xslx file, and would like to add a table over that data. I would also like to keep the headers that I have already written, instead of adding them again. Is t…

Unexpected tokens in !DOCTYPE html in PyCharm Community Edition

I am new in using PyCharm but I am loving it gradually. I am getting a red underline on <!DOCTYPE html> and the error is "Unexpected Token".Why PyCharm shows it? I cant understand.

Indexing a numpy array with a numpy array of indexes [duplicate]

This question already has answers here:Indexing a numpy array with a list of tuples(2 answers)Index multidimensional array with index array(1 answer)Closed 5 years ago.I have a 3D numpy array data and …

Input redirection with python

I have the following program to test input redirection in Python.a = int(raw_input("Enter a number: ")) b = raw_input("Enter a string: ") print "number entered = ", a prin…

TypeError: can only concatenate str (not numpy.int64) to str

I want to print the variable based on the index number based on the following dataset:Here I used the following code:import pandas as pdairline = pd.read_csv("AIR-LINE.csv")pnr = input("…

Saving scatterplot animations with matplotlib produces blank video file

I am having a very similar problem to this questionbut the suggested solution doesnt work for me. I have set up an animated scatter plot using the matplotlib animation module. This works fine when it i…