Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added an asyncio server example #165

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

evalott100
Copy link

I can't get the async server to work. I think adding an example while troubleshooting this would be good.

Both READ_PV and WRITE_PV can be successfully set up and pvget'd, however I can't get the async handler to function. The pvput times out, and after exiting the server we get the error:

task: <Task pending name='Task-4' coro=<AttrWHandler.put() running at /.../p4p/example/asyncio_server.py:31> ... RuntimeWarning: coroutine 'AttrWHandler.put' was never awaited

I was judging this was the recommended way to set up a server from asynciotest.py?

async def _run(self) -> None:
logging.info("Running server.")
try:
Server.forever(providers=[self.provider._provider])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asyncio code should not use Server.forever(), which is a convenience for threading code to park a main thread. It probably should have been added under Server.thread. I think it actually pre-dates my adding support for cothread/asyncio.

The doc. comment gives the basic template.

with Server(*args, **kws):
try;
time.sleep(99999999)
except KeyboardInterrupt:
pass

A direct equivalent with asyncio would replace the blocking sleep.

             with Server(*args, **kws): 
                 await asyncio.sleep(99999999) 

Since KeyboardInterrupt doesn't work with asyncio, a more complete version would handle signals to allow for a graceful shutdown. (an example)

             with Server(*args, **kws): 
                 done = asyncio.Event()
                 loop.add_signal_handler(signal.SIGINT, done.set)
                 loop.add_signal_handler(signal.SIGTERM, done.set)
                 await done.wait()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay that makes sense. The example works now.

@evalott100 evalott100 force-pushed the add-asyncio-server-example branch from 8c94458 to b698f52 Compare February 3, 2025 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants