Python Forum
Error: There is no current event loop in thread 'MainThread'
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error: There is no current event loop in thread 'MainThread'
#1
Exclamation 
Python 3.7.2

In my processing, the main function is wrapped by error interceptors and the interception is performed correctly:
try:
    downloader = Downloader.Downloader()
    downloader.download()
except Exception as err:
    General.exist_error = True
    Events.Sender.send_error(err)
But if an error occurred (and was correctly intercepted!), then the following start of processing fall down on the line:
self.event_exist_data = Event()
with error:
Error:
There is no current event loop in thread 'MainThread'
I found the place of the code in which the error occurs (module events):
class BaseDefaultEventLoopPolicy(AbstractEventLoopPolicy):
...
    def get_event_loop(self):
        """Get the event loop.

        This may be None or an instance of EventLoop.
        """
        if (self._local._loop is None and #TRUE
                not self._local._set_called and #FALSE BECAUSE self._local._set_called IS TRUE !!!
                isinstance(threading.current_thread(), threading._MainThread)): #TRUE
            self.set_event_loop(self.new_event_loop())

        if self._local._loop is None:
            raise RuntimeError('There is no current event loop in thread %r.' #On the second, third ... iteration I get here!!!
                               % threading.current_thread().name)

        return self._local._loop
Reply
#2
I think you'll need to show more code. Especially where/how you're using event loops. Without seeing that, we can't really guess why there's a problem with the event loop.
Reply
#3
Thank you for your interest!
There is no low-level work with the event loop anywhere, just the use of the run() method in a single place (presented below). In my code specific classes are wrapped by decorators who register them for execution in the REGISTRY list (it is currently only 1 class):
class AbstaractUnit:
    def __init__(self):
        self.fill_coroutines()
        
    @abstractmethod    
    def fill_coroutines(self): pass
    
    async def _create_workers(self) -> None:
        await asyncio.gather(*self.coroutines)
        
    def __call__(self) -> None:
        asyncio.run(self._create_workers())

class Downloader(_Unit.AbstaractUnit):
    REGISTRY = []
    
    @classmethod
    def register(cls):
        def decorator(child_class):
            cls.REGISTRY.append(child_class)
            return child_class
        
        return decorator
    
    def __init__(self):
        _Unit.AbstaractUnit.__init__(self)
        
    def fill_coroutines(self):
        self.coroutines = []
        for Method in self.REGISTRY:
            coroutine = Method()()
            self.coroutines.append(coroutine)
            
    def download(self):
        self.__call__()
Reply
#4
In this way, I run the code for execution and intercept errors:
try:
            downloader = Downloader.Downloader()                
            downloader.download()
        except Exception as err:
            General.exist_error = True
            Events.Sender.send_error(err)
Reply
#5
Is there any way you can put together a complete example I can run, that demonstrates the issue?
The only thing I can see, is that the manager/AbstractUnit is calling asyncio.run(), which might not be safe to call more than once.

I get that from the docs: https://docs.python.org/3/library/asynci...syncio.run
Quote:This function cannot be called when another asyncio event loop is running in the same thread.
...
Quote:This function always creates a new event loop and closes it at the end. It should be used as a main entry point for asyncio programs, and should ideally only be called once.
Reply
#6
(Feb-20-2019, 08:21 PM)nilamo Wrote: I get that from the docs: https://docs.python.org/3/library/asynci...syncio.run
Quote:This function cannot be called when another asyncio event loop is running in the same thread.

Thank you!
I replaced:
asyncio.run(self._create_workers())
on this lines:
ioloop = asyncio.get_event_loop()
ioloop.run_until_complete(self._create_workers())
and the error, that occurred when the processing was restarted, disappeared.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error: can't start new thread maha2 0 1,439 Jun-13-2023, 12:26 PM
Last Post: maha2
  help RuntimeError: no running event loop marpaslight 5 3,712 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  A question about 'Event loop is closed' fc5igm 2 2,201 Oct-05-2021, 02:00 AM
Last Post: fc5igm
  bleak library RuntimeError: This event loop is already running alice93 3 4,081 Sep-30-2021, 08:06 AM
Last Post: alice93
  Pool multiprocessing - know current status in loop? korenron 0 1,632 Jul-28-2021, 08:49 AM
Last Post: korenron
  RuntimeError: This event loop is already running newbie2019 2 6,950 Sep-30-2020, 06:59 PM
Last Post: forest44
  Cant find root cause of thread.lock error burlyboys 0 1,547 May-18-2020, 12:51 PM
Last Post: burlyboys
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,559 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  Com Error with macro within for loop due to creating new workbook in the loop Lastwizzle 0 1,360 May-18-2019, 09:29 PM
Last Post: Lastwizzle
  How to add coroutine to a running event loop? AlekseyPython 1 8,143 Mar-21-2019, 06:04 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020