r/PythonLearning • u/XxPopePiusxX • Aug 06 '26
Whats the best way to have two loops run concurrently nested in another function returning one value as if running a race
Edit: I’m making a project in where I’m having 4 distinct loops that’s act as if there racing. I want them all the run concurrently inside one function so whenever one of them reaches a specific value all 4 “stop” and the winner is returned.
I was using multiprocessing but had a lot of trouble nesting the function with that so stopped. Now I’m using asyncio and want to know if that’s the right tool for the job or if I should use threading.
Trying to use AI as little as possible for my own learning.
8
Upvotes
5
u/denehoffman Aug 06 '26
Can you be a bit more specific? Are you trying to trigger a race condition, or are you trying to benchmark something, or some other third thing?
4
u/johlae Aug 06 '26 edited Aug 06 '26
Something like this, from your vague description:
1
u/Excellent-Practice Aug 06 '26
You might want to share more details about your intent or use case. Broadly speaking, it sounds like you want to use threads. Your computer can only really do one thing at a time, but it can switch between tasks while it is waiting for inputs or responses.
0
u/WorriedTumbleweed289 Aug 06 '26
Google python concurrency. The AI answer gives 3 methods of creating concurrency depending on what you are trying to do and what resources are bound.
Their choises are,
Depending on what you are trying to do, Writing yield functions and putting them in loops may be what you want.