简体   繁体   English

在python(pika库)中并行运行代码

[英]run code in parallel in python (pika library)

i have a pika receiver that receives commands and executes another python script.我有一个 pika 接收器,它接收命令并执行另一个 python 脚本。 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.如果我通过 mqtt 协议收到命令,它会等到它完成“make.py”函数再执行它。 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()

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.我想你不想等待make.py完成执行,您可以使用subprocess.Popen函数从Python的subprocess模块。

You can refer more about Popen function at docs您可以在文档中参考有关Popen函数的更多信息

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}"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有 python 鼠兔库的 Rabbit MQ StreamLostError - Rabbit MQ StreamLostError with python pika library 一种并行运行一段python代码的简单方法? - A simple way to run a piece of python code in parallel? 在多个 AWS 实例上运行并行 Python 代码 - Run parallel Python code on multiple AWS instances 如何使用python调度程序库运行并行任务? - How to use python scheduler library to run parallel tasks? 使用 Python 鼠兔库合并 RabbitMQ 队列的最佳方法是什么? - What is the best way to merge RabbitMQ queues using Python pika library? 需要使Python / Pika代码更健壮-不稳定的连接和高延迟 - Need to make Python / Pika code robust - unstable connection and high latency 从一个python脚本并行运行代码/任务? - run code/tasks in parallel from one python script? 如何使用 python matplotlib 与另一个代码并行运行 animation? - How to run animation in parallel with another code with python matplotlib? Python:Joblib中的并行处理使代码运行更加缓慢 - Python: Parallel Processing in Joblib Makes the Code Run Even Slower Python MySQL队列:并行运行代码/查询,但分别提交 - Python MySQL queue: run code/queries in parallel, but commit separately
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM