[英]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.