Search code examples
pythonmultithreadingpython-multithreading

run code in parallel in python (pika library)


i have a pika receiver that receives commands and executes another python script. the problem is i'm unable to run the script in parallel as multiprocess or threading. if i receive the command via the mqtt protocol, it waits until it finishes the "make.py" function to execute it again. i wanted it to run in parallel. someone can help?

def call_mkdt(ch, method, properties, body):
    os.system(f"make.py {body}")


def consume():
    channel.basic_consume(queue='UploadCompleted', on_message_callback=call_mkdt, auto_ack=True)
    print(' [*] ETL')
    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()

if __name__== "__main__":
    p1 = threading.Thread(name="Hello1", target=consume)
    p1.start()

Solution

  • I suppose you don't want to wait for the make.py to finish the execution, You can use subprocess.Popen function from python's subprocess module.

    You can refer more about Popen function at docs

    Replace:

    def call_mkdt(ch, method, properties, body):
        os.system(f"make.py {body}")
    

    With:

    import subprocess
    
    def call_mkdt(ch, method, properties, body):
        subprocess.Popen(["make.py", f"{body}"])