uAgents icon indicating copy to clipboard operation
uAgents copied to clipboard

There is no current event loop in thread 'ScriptRunner.scriptThread' Error

Open jrriehl opened this issue 2 years ago • 4 comments
trafficstars

Moved from here: https://github.com/fetchai/cosmpy/discussions/360

i think asyncio.get_event_loop() method works within uagents module ? and how to call asyncio.get_event_loop() function in uagents module?

class ASGIServer: def init( self, port: int, loop: asyncio.AbstractEventLoop, queries: Dict[str, asyncio.Future], logger: Optional[Logger] = None, ): self._port = int(port) self._loop = loop self._queries = queries self._logger = logger or get_logger("server") self._server = None there is no get method so sadly frowning because of that, it conflicts to streamlit module and i get the following error:

File "C:\Users\selcu\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 656, in get_event_loop raise RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'.

jrriehl avatar Jun 05 '23 09:06 jrriehl

What command are you running that results in this error?

Normally, when you run agent.run(), an event loop will be started if there is none currently running.

But in general, you should be able to use asyncio.get_event_loop() to get an event loop independent of any module.

jrriehl avatar Jun 05 '23 09:06 jrriehl

I work on the script: (it is recomended by fetchai in the link https://docs.fetch.ai/uAgents/simple-interaction/#add-a-second-agent)

lets say it agents.py. in the terminal, when I run python agents.py it works well.

My problem is the usage of this code in a Python framework (web frameworks). In any framework, when a state change is a must, I get the error "get_event_loop raise RuntimeError('There is no current event loop in thread %r.' RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'". So I thouhgt that ı need to call asyncio.get_event_loop(). I try to visulate ctx.logger.info in a print method (not a console (window) log or any console print (terminal) method). when i tried to use your script by modifing the framework, it gave the error above.

from uagents import Agent, Context, Bureau

alice = Agent(name="alice", seed="alice recovery phrase") bob = Agent(name="bob", seed="bob recovery phrase")

@alice.on_interval(period=2.0) async def say_hello(ctx: Context): ctx.logger.info(f'Hello, my name is {ctx.name}')

@bob.on_interval(period=2.0) async def say_hello(ctx: Context): ctx.logger.info(f'Hello, my name is {ctx.name}')

bureau = Bureau() bureau.add(alice) bureau.add(bob)

if name == "main": bureau.run()

selcuktopal80 avatar Jun 05 '23 13:06 selcuktopal80

Thanks for the extra info! It's not clear to me which web framework you are using, but for some web frameworks it helps to install nest_asycnio and run the following at the top of your script:

import nest_asyncio
nest_asyncio.apply()

So that might be worth a try. If that doesn't work, can you say which particular web framework you are using?

jrriehl avatar Jun 05 '23 13:06 jrriehl

Thanks for the extra info! It's not clear to me which web framework you are using, but for some web frameworks it helps to install nest_asycnio and run the following at the top of your script:

import nest_asyncio
nest_asyncio.apply()

So that might be worth a try. If that doesn't work, can you say which particular web framework you are using?

I am working on streamlit dapp, it has own asyncio function inside, so uagents module and streamlit are incompatible sadly.

your recommendation did not solve the problem.

selcuktopal80 avatar Jun 06 '23 10:06 selcuktopal80

@selcuktopal80 I'm working on a project very similar to yours, did you fixed the problem? If so, how?

raphavtorres avatar Jun 27 '24 23:06 raphavtorres

Please follow up the discussion in #124 and see if the already existing solution helps or if we'll need to prioritise the latter where an agent can attach to an already running loop.

Archento avatar Jul 24 '24 10:07 Archento