gpt4 book ai didi

python - Django 从 celery 的 subprocess.Popen 获得干净的返回

转载 作者:行者123 更新时间:2023-12-01 08:21:35 24 4
gpt4 key购买 nike

我有一个调用 subprocess.Popen 又名 shell cmd 的 celery 任务,但由于某种原因,cmd 的输出没有保存到用户字段,而是保存到进程内存 id !?!?

[2019-02-09 21:35:05,526: INFO/MainProcess] Received task: MyProject.tasks.allocate_new_btc_address[08d7db34-5710-4224-be07-c968599d9039]  
[2019-02-09 21:35:05,540: WARNING/ForkPoolWorker-9] <subprocess.Popen object at 0x7f31063d79b0>
[2019-02-09 21:35:05,540: INFO/ForkPoolWorker-9] MyProject.tasks.allocate_new_btc_address[08d7db34-5710-4224-be07-c968599d9039]: New BTC address has been allocated to a user account
[2019-02-09 21:35:05,540: INFO/ForkPoolWorker-9] Task MyProject.tasks.allocate_new_btc_address[08d7db34-5710-4224-be07-c968599d9039] succeeded in 0.012948354000400286s: None
iubcasiuiuBIUcb723iub7c8732iewe

任务.py

@app.task
def allocate_new_btc_address(user_pk):
user = User.objects.get(pk=user_pk)
new_address = subprocess.Popen(['electrum', 'createnewaddress'])
try:
user.acc_btc_addr = new_address
user.save()
print(new_address)
logger.info("New BTC address has been allocated to a user account")
except Exception as e:
print(e)

这就是保存到字段中的值:

<subprocess.Popen object at 0x7f31063d79b0>

提前致谢

最佳答案

Popen 返回一个对象。要直接获取输出,请使用 subprocess.check_output() 函数:

from subprocess import check_output
new_address = check_output(['electrum', 'createnewaddress'])

关于python - Django 从 celery 的 subprocess.Popen 获得干净的返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611035/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com