splinter
splinter

Reputation: 1

Start an async process - can/should I use asyncio?

Here's what I'm trying to achieve: there's a long running process, i.e. rabbitmq consumer. When it receives a specific message, it should start a parallel process that does something (whatever... print, sleep for a minute...) and finishes. In the meantime, the main process still consumes messages.

This scenario could be simplified to this example:

import time

@this_should_run_in_parallel
def background_foo():
    for _ in range(3):
        print('zxc')
        time.sleep(1)

for k in range(10):
    print('abc')
    if k == 5:
        background_foo()
    time.sleep(1)

# for loop is printing abc
# but once k == 5 the parallel function also start printing
# for loop goes on
# desired output

abc
abc
abc
abc
abc
zxc
abc
zxc
abc
zxc
abc
abc
abc

Is this something asyncio should be used for, or should I stick to multiprocessing/threading modules?

Upvotes: 0

Views: 214

Answers (1)

Vincent
Vincent

Reputation: 13415

Yes asyncio can deal with that, but you have to pick the right tool depending on the nature of the task:

Asyncio flow chart

Links:

Upvotes: 1

Related Questions