python cronjob中的多线程问题
我有一个 python 程序,它使用 ThreadPool 进行多线程处理。该程序是 shell 脚本中的一个步骤。当我在命令行上手动执行 shell 脚本时,整个流程按预期工作。但是,当我将 shell 脚本作为 cronjob 执行时,似乎流程在 python 多线程步骤完全完成之前就进入了后续步骤。
在 python 程序中,我确实调用 AsyncResult.get(timeout) 来等待所有结果返回,然后再继续。
I have a python program that uses the ThreadPool for multithreading. The program is one step in a shell script. When I execute the shell script manually on the command line, the entire flow works as expected. However, when I execute the shell script as a cronjob, it appears that the flow goes to the next steps before the python multithreading steps are completely finished.
Inside the python program, I do call AsyncResult.get(timeout) to wait for all the results to come back before moving on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在
crontab
中设置"TERM=xterm"
(或您拥有的任何环境变量,通过终端上的命令“env”计算)。Try setting
"TERM=xterm"
(or whatever env variable you have, figure out by command 'env' on your terminal) in yourcrontab
.还可以通过
batch(1)
运行您的程序(请参阅命令man batch
的输出)。如果可以正常工作,但 cron 版本却不能,那么几乎可以肯定是您的环境变量设置有问题。要验证这一点,请从交互式 shell 运行printenv
以检查您的环境。然后在 crontab 中执行相同的操作(您只需要临时为其设置一个额外的 cron 条目)。在调用 Python 之前尝试在 shell 脚本中设置变量。另一方面,如果它也无法通过
batch(1)
工作,则可能与您的代码打开的文件有关。尝试使用从 /dev/null 重定向的输入并将输出发送到文件来运行 shell 脚本:Run your program via
batch(1)
(see the output of the commandman batch
) as well. If that works OK, but the cron version does not, then it is almost certainly a problem with your environment variable setup. To verify that, runprintenv
from your interactive shell to inspect your environment there. Then do the same thing inside the crontab (you will just need to temporarily set up an extra cron entry for it). Try setting the variables in your shell script before invoking Python.On the other hand, if it doesn't work via
batch(1)
either, it could be something to do with the files that your code has open. Try running your shell script with input redirected from /dev/null and output going to a file: