在单独的进程中运行 python [英] Run python in a separate process

查看:74
本文介绍了在单独的进程中运行 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个快速的 bash 脚本或程序,它可以让我在单独的进程中启动 python 脚本.做到这一点的最佳方法是什么?我知道这非常简单,只是想知道是否有首选的方法来做到这一点.

I'm looking for a quick bash script or program that will allow me to kick off a python script in a separate process. What's the best way to do this? I know this is incredibly simple, just curious if there's a preferred way to do it.

推荐答案

只需使用与号 (&) 即可在后台启动 Python 进程.Python 已经在与 BASH 脚本不同的进程中执行,因此说在单独的线程中"运行它没有多大意义——我假设您只是希望它在后台运行:

Just use the ampersand (&) in order to launch the Python process in the background. Python already is executed in a separate process from the BASH script, so saying to run it "in a separate thread" doesn't make much sense -- I'm assuming you simply want it to run in the background:

#! /bin/bash
python path/to/python/program.py &

请注意,上述内容可能会导致将文本打印到控制台.您可以通过使用重定向将 stdout 和 stderr 重定向到文件来解决此问题.例如:

Note that the above may result in text being printed to the console. You can get around this by using redirection to redirect both stdout and stderr to a file. For example:

#! /bin/bash
python path/to/python/program.py > results.txt 2> errors.log &

这篇关于在单独的进程中运行 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆