keijack / python-eureka-client

A eureka client written in python. Support registering your python component to Eureka Server, as well as calling remote services by pulling the the Eureka registry.
MIT License
183 stars 43 forks source link

Cannot run the event loop while another loop is running #70

Closed haniDjebbi closed 1 year ago

haniDjebbi commented 2 years ago

i created a flask application and am runing it using jupyter notebook. when i try to connect it to an eureka server i get the following error: 612 """Run until the Future is done. 613 614 If the argument is a coroutine, it is wrapped in a Task. (...) (...) 620 Return the Future's result, or raise its exception. 621 """ 622 self._check_closed() --> 623 self._check_running() 625 new_task = not futures.isfuture(future) 626 future = tasks.ensure_future(future, loop=self)

File D:\anaconda3\envs\ml_env01\lib\asyncio\base_events.py:585, in BaseEventLoop._check_running(self) 583 raise RuntimeError('This event loop is already running') 584 if events._get_running_loop() is not None: --> 585 raise RuntimeError( 586 'Cannot run the event loop while another loop is running')

RuntimeError: Cannot run the event loop while another loop is running 620 Return the Future's result, or raise its exception. 621 """ 622 self._check_closed() --> 623 self._check_running() 625 new_task = not futures.isfuture(future) 626 future = tasks.ensure_future(future, loop=self)

File D:\anaconda3\envs\ml_env01\lib\asyncio\base_events.py:585, in BaseEventLoop._check_running(self) 583 raise RuntimeError('This event loop is already running') 584 if events._get_running_loop() is not None: --> 585 raise RuntimeError( 586 'Cannot run the event loop while another loop is running')

RuntimeError: Cannot run the event loop while another loop is running

and here is the code snippet that am using :

rest_port = 6100
if __name__ == "__main__":
    eureka_client.init(eureka_server="http://localhost:8761/eureka",
                   app_name="pdf-miner",
                   instance_port=rest_port)
    app.run(port=rest_port)
tducthang commented 1 year ago

@haniDjebbi I have same problem and reinstall version 0.10.0 --> Fixed . You can try it !!!!

keijack commented 1 year ago

Please check this answer: https://stackoverflow.com/questions/55409641/asyncio-run-cannot-be-called-from-a-running-event-loop-when-using-jupyter-no

You can run your code in aysnc mode:

rest_port = 6100
async def main():
    await eureka_client.init_async(eureka_server="http://localhost:8761/eureka",
               app_name="pdf-miner",
               instance_port=rest_port)
    app.run(port=rest_port)
await main()
tducthang commented 1 year ago

oh, thank you. It works :star_struck:

sdhzlzhk commented 1 year ago

@keijack I faced same problem. I start a django web service with gunicorn[gevent]. I register eureka server with init, and do_service to recall other service occured the RuntimeError. Traceback (most recent call last): File "/usr/src/app/PythonToolService/terrainPointRequest.py", line 198, in genHeat eureka_client.do_service(app_name="xxxxxx", service="xxxxxxxxx",prefer_ip=True, timeout=60) File "/usr/local/lib/python3.9/site-packages/py_eureka_client/eureka_client.py", line 1310, in do_service return get_event_loop().run_until_complete(do_service_async(app_name=app_name, service=service, return_type=return_type, File "/usr/local/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete self._check_running() File "/usr/local/lib/python3.9/asyncio/base_events.py", line 578, in _check_running raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running /usr/local/lib/python3.9/concurrent/futures/thread.py:79: RuntimeWarning: coroutine 'do_service_async' was never awaited del work_item I read the eureka_client.py ,Maybe the gloabl _event_loop caused, So I rewrite with threadLocal and run success. image

keijack commented 1 year ago

@keijack I faced same problem. I start a django web service with gunicorn[gevent]. I register eureka server with init, and do_service to recall other service occured the RuntimeError. Traceback (most recent call last): File "/usr/src/app/PythonToolService/terrainPointRequest.py", line 198, in genHeat eureka_client.do_service(app_name="xxxxxx", service="xxxxxxxxx",prefer_ip=True, timeout=60) File "/usr/local/lib/python3.9/site-packages/py_eureka_client/eureka_client.py", line 1310, in do_service return get_event_loop().run_until_complete(do_service_async(app_name=app_name, service=service, return_type=return_type, File "/usr/local/lib/python3.9/asyncio/base_events.py", line 618, in run_until_complete self._check_running() File "/usr/local/lib/python3.9/asyncio/base_events.py", line 578, in _check_running raise RuntimeError('This event loop is already running') RuntimeError: This event loop is already running /usr/local/lib/python3.9/concurrent/futures/thread.py:79: RuntimeWarning: coroutine 'do_service_async' was never awaited del work_item I read the eureka_client.py ,Maybe the gloabl _event_loop caused, So I rewrite with threadLocal and run success. image

The global event_loop is moved to thread.local in 0.18.4.

keijack commented 1 year ago

Close for long time silence.