django-tasks icon indicating copy to clipboard operation
django-tasks copied to clipboard

Running async tasks in parallel

Open adnathanail opened this issue 10 months ago • 4 comments

Really love this package! Can't wait to see it land in Django core

I was wondering if it was possible to run tasks in parallel?

I have some slow network calls which I call asynchronously, but tasks are still executed sequentially. Is it possible to start processing the next task while awaiting a call in a previous one?

adnathanail avatar Feb 24 '25 03:02 adnathanail

Currently no - each process runs just a single task concurrently. If you need to run multiple tasks at once, you'll need to run multiple worker processes.

It's something I think it would be great to support, particularly for async tasks, but working out the behaviour, especially when relating to non-async tasks is complex.

See also this discussion: https://github.com/RealOrangeOne/django-tasks/discussions/117

RealOrangeOne avatar Feb 24 '25 10:02 RealOrangeOne

Ahh gotcha! Ok thank you

adnathanail avatar Feb 24 '25 10:02 adnathanail

[...] especially when relating to non-async tasks is complex.

Can you give a brief explanation or pointer what you mean with that? Having a sync task in the mix would be blocking other tasks, even if all other tasks are async, but that could also happen if all tasks are completely async. Also, I'm not sure if there is a clean way to just use threads instead of an event loop. Anything that adds some concurrency without spawning full processes (and exploding memory requirements) sounds fine to me.

oliverhaas avatar Mar 21 '25 21:03 oliverhaas

There are certain features which may require child processes for tasks (eg timeouts for sync tasks). So the only way to achieve proper concurrency is to do process-based. The more complexity in the worker process, the harder it is to reason about what it's doing.

I could see an argument for an async-only worker command, which only handles async tasks, and thus has massively improved throughput. But that's a future aspiration.

RealOrangeOne avatar Mar 31 '25 08:03 RealOrangeOne