fetchai / uAgents

A fast and lightweight framework for creating decentralized agents with ease.
Apache License 2.0
883 stars 231 forks source link

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

Closed jrriehl closed 2 months ago

jrriehl commented 1 year ago

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 commented 1 year ago

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.

selcuktopal80 commented 1 year ago

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()

jrriehl commented 1 year ago

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?

selcuktopal80 commented 1 year ago

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.

raphavtorres commented 3 months ago

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

Archento commented 2 months ago

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.