Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RunTimeError in Twint : This event loop is already running

Tags:

python

I use a jupyter notebook and run twint.

Code

c = twint.Config()
c.Username = 'twitter'
c.Limit = 20
twint.run.Followers(c)

The above commands produce a runtime error relating to (I believe) the code attempting to create 2 asynchronous event loops. Traceback as follows:

RuntimeError                            Traceback (most recent call last)
<ipython-input-4-4713cc05fb59> in <module>()
----> 1 twint.run.Favorites(c)

~/coding/tools/twint/twint/run.py in Favorites(config)
    119 def Favorites(config):
    120     config.Favorites = True
--> 121     run(config)
    122 
    123 def Followers(config):

~/coding/tools/twint/twint/run.py in run(config)
    115 
    116 def run(config):
--> 117     get_event_loop().run_until_complete(Twint(config).main())
    118 
    119 def Favorites(config):

~/.pyenv/versions/3.6.5/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
    453         future.add_done_callback(_run_until_complete_cb)
    454         try:
--> 455             self.run_forever()
    456         except:
    457             if new_task and future.done() and not future.cancelled():

~/.pyenv/versions/3.6.5/lib/python3.6/asyncio/base_events.py in run_forever(self)
    407         self._check_closed()
    408         if self.is_running():
--> 409             raise RuntimeError('This event loop is already running')
    410         if events._get_running_loop() is not None:
    411             raise RuntimeError(

RuntimeError: This event loop is already running
like image 784
haru86 Avatar asked May 18 '21 11:05

haru86


People also ask

Why can’t i re-enter an event loop in Python?

It really seems like an implementation oversight in Python. Not allowing re-entrance of the event loop has the toxic side-effect that any code that needs to call and async function must itself be async (unless its running in another thread). – user48956 Jan 14 '20 at 17:48

What is event loop running in async programming?

An example usage related to this would be helpful – alper Nov 16 at 15:24 | Show 3more comments 46 Event loop running - is an entry point of your async program. It manages running of all coroutines, tasks, callbacks.

How many Python aiohttp into an existing event loop?

12 python aiohttp into existing event loop 11 Asyncio in corroutine RuntimeError: no running event loop 3 RuntimeError: Event loop is closed in asyncpraw python Hot Network Questions Why does a jumping rope have no bending stiffness and compression stiffness? QGIS can I skip invalid geometries or do I have to fix them?

Can asyncio event loop be nested?

May 11 at 9:28 Citing nest_asyncio: By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it’s impossible to run tasks and wait for the result.


Video Answer


1 Answers

I have found a solution for Jupyter notebooks using the nest_async

Simply do

pip install nest_asyncio

And add these lines.

import nest_asyncio
nest_asyncio.apply()
like image 156
saya Avatar answered Oct 16 '22 09:10

saya