Closed jrriehl closed 2 months 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.
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()
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?
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 I'm working on a project very similar to yours, did you fixed the problem? If so, how?
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.
Moved from here: https://github.com/fetchai/cosmpy/discussions/360