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

Create a background task in event loop #154

Open
EnricoBeltramo opened this issue Jan 14, 2024 · 5 comments
Open

Create a background task in event loop #154

EnricoBeltramo opened this issue Jan 14, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@EnricoBeltramo
Copy link

Is your feature request related to a problem? Please describe.
I would create a background task to do some timerized operation i.e. periodical send some data update to all clients connected

Describe the solution you'd like
something like app.create_background_task(callback)

@cirospaciari cirospaciari added the enhancement New feature or request label Feb 1, 2024
@cirospaciari
Copy link
Owner

I will take this in consideration so in the native extension version we can have this feature added

@rbhuta
Copy link

rbhuta commented Apr 12, 2024

+1, is there any workaround to get a periodic task to fire?

@cirospaciari
Copy link
Owner

+1, is there any workaround to get a periodic task to fire?

You can register it in the default loop in asyncio using pure python for now

@rbhuta
Copy link

rbhuta commented Apr 12, 2024

I tried a few ways to get it to register, but I keep getting errors and I think I'm not handling the registration properly. I keep seeing errors like "Cannot run the event loop while another loop is running." Could you maybe help me with a simple example? How could I add the bg_task to the loop below? Thanks - a bit new to asyncio loops and want to make sure to get this right without degrading the performance of socketify (which is incredible btw)

from socketify import App,
app = App()

async def bg_task():
    while True:
        await asyncio.sleep(60)
        logger.info("Running task")


def start():
    app.listen(PORT, HOST)
    logger.info(f"Listening on http://{HOST}:{PORT}")
    # how do i add the the thread 
    app.run()


if __name__ == '__main__':
    start()

@megaterium
Copy link

megaterium commented Apr 12, 2024

I tried a few ways to get it to register, but I keep getting errors and I think I'm not handling the registration properly. I keep seeing errors like "Cannot run the event loop while another loop is running." Could you maybe help me with a simple example? How could I add the bg_task to the loop below? Thanks - a bit new to asyncio loops and want to make sure to get this right without degrading the performance of socketify (which is incredible btw)

from socketify import App,
app = App()

async def bg_task():
    while True:
        await asyncio.sleep(60)
        logger.info("Running task")


def start():
    app.listen(PORT, HOST)
    logger.info(f"Listening on http://{HOST}:{PORT}")
    # how do i add the the thread 
    app.run()


if __name__ == '__main__':
    start()

Not sure if this is what you wanted to achieve. If it helps, I have this socketify GPT that helps me a lot! https://chat.openai.com/g/g-ZSUkRkncp-socketify-doc-assistant

import asyncio
import threading
from socketify import App
import logging

logger = logging.getLogger(__name__)
app = App()

async def bg_task():
    while True:
        print("Background task is running...")
        await asyncio.sleep(2)
        logger.info("Background task running")

def run_socketify_app():
    app.listen(3050, 'localhost')
    print("Socketify app is running...")
    logger.info("Listening on http://localhost:3050")
    app.run()

def run_bg_task():
    asyncio.run(bg_task())

def start():
    # Run background task in a separate thread
    thread = threading.Thread(target=run_bg_task)
    thread.start()

    # Run Socketify app in the main thread
    run_socketify_app()

if __name__ == '__main__':
    start()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants