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

Avoid busy loop in workers, while waiting for work. #214

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

Conversation

briot
Copy link

@briot briot commented Feb 7, 2025

My application has a main loop that basically looks like

    async with aiomultiprocess.Pool(
        processes=Global.PROCESSES,
        initializer=Global.create,
        initargs=(args, Global.psql, openai_factory),
        maxtasksperchild=0,   # kill processes after those tasks
        childconcurrency=Global.CHILD_CONCURRENCY,
        queuecount=Global.QUEUES,
        scheduler=scheduler,
        loop_initializer=uvloop.new_event_loop,
    ) as pool:
        monitor = Monitor(pool, scheduler=scheduler, job_factory=job_factory)

        async with asyncio.TaskGroup() as tg:
            _ = tg.create_task(monitor.listen()

The last part is listening for postgresql notifications when database tables change, and then calling queue_work(). One minor issue is that the worker processes are in fact doing busy looping (with a small 0.005s delay) while calling get_no_wait on the queue repeatedly. On the other hand, if the worker is not handling any pending work, we might as well call a blocking get while waiting for initial work.

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.

1 participant