• <i id='UXOf0'><tr id='UXOf0'><dt id='UXOf0'><q id='UXOf0'><span id='UXOf0'><b id='UXOf0'><form id='UXOf0'><ins id='UXOf0'></ins><ul id='UXOf0'></ul><sub id='UXOf0'></sub></form><legend id='UXOf0'></legend><bdo id='UXOf0'><pre id='UXOf0'><center id='UXOf0'></center></pre></bdo></b><th id='UXOf0'></th></span></q></dt></tr></i><div id='UXOf0'><tfoot id='UXOf0'></tfoot><dl id='UXOf0'><fieldset id='UXOf0'></fieldset></dl></div>

    <small id='UXOf0'></small><noframes id='UXOf0'>

    • <bdo id='UXOf0'></bdo><ul id='UXOf0'></ul>
    <tfoot id='UXOf0'></tfoot>
  • <legend id='UXOf0'><style id='UXOf0'><dir id='UXOf0'><q id='UXOf0'></q></dir></style></legend>

        使用 Popen 打开进程并获取 PID

        Opening a process with Popen and getting the PID(使用 Popen 打开进程并获取 PID)
        <legend id='ktc44'><style id='ktc44'><dir id='ktc44'><q id='ktc44'></q></dir></style></legend>

          <tbody id='ktc44'></tbody>
        <tfoot id='ktc44'></tfoot>

              • <small id='ktc44'></small><noframes id='ktc44'>

                <i id='ktc44'><tr id='ktc44'><dt id='ktc44'><q id='ktc44'><span id='ktc44'><b id='ktc44'><form id='ktc44'><ins id='ktc44'></ins><ul id='ktc44'></ul><sub id='ktc44'></sub></form><legend id='ktc44'></legend><bdo id='ktc44'><pre id='ktc44'><center id='ktc44'></center></pre></bdo></b><th id='ktc44'></th></span></q></dt></tr></i><div id='ktc44'><tfoot id='ktc44'></tfoot><dl id='ktc44'><fieldset id='ktc44'></fieldset></dl></div>
                  <bdo id='ktc44'></bdo><ul id='ktc44'></ul>
                  本文介绍了使用 Popen 打开进程并获取 PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在开发一个漂亮的小功能:

                  I'm working on a nifty little function:

                  def startProcess(name, path):
                      """
                      Starts a process in the background and writes a PID file
                  
                      returns integer: pid
                      """
                  
                      # Check if the process is already running
                      status, pid = processStatus(name)
                  
                      if status == RUNNING:
                          raise AlreadyStartedError(pid)
                  
                      # Start process
                      process = subprocess.Popen(path + ' > /dev/null 2> /dev/null &', shell=True)
                  
                      # Write PID file
                      pidfilename = os.path.join(PIDPATH, name + '.pid')
                      pidfile = open(pidfilename, 'w')
                      pidfile.write(str(process.pid))
                      pidfile.close()
                  
                      return process.pid
                  

                  问题在于 process.pid 不是正确的 PID.它似乎总是比正确的PID低1.例如,它说进程从 31729 开始,但 ps 说它在 31730 运行.每次我尝试它都会关闭 1.我猜它返回的 PID 是当前 进程,而不是启动的进程,并且新进程获得高 1 的下一个"pid.如果是这种情况,我不能仅仅依靠返回 process.pid + 1 因为我不能保证它总是正确的.

                  The problem is that process.pid isn't the correct PID. It seems it's always 1 lower than the correct PID. For instance, it says the process started at 31729, but ps says it's running at 31730. Every time I've tried it's off by 1. I'm guessing the PID it returns is the PID of the current process, not the started one, and the new process gets the 'next' pid which is 1 higher. If this is the case, I can't just rely on returning process.pid + 1 since I have no guarantee that it'll always be correct.

                  process.pid为什么不返回新进程的PID,如何实现我想要的行为?

                  Why doesn't process.pid return the PID of the new process, and how can I achieve the behaviour I'm after?

                  推荐答案

                  来自 http://docs.python.org/library/subprocess.html:

                  Popen.pid 子进程的进程ID.

                  Popen.pid The process ID of the child process.

                  注意,如果你将shell参数设置为True,这就是进程生成的 shell 的 ID.

                  Note that if you set the shell argument to True, this is the process ID of the spawned shell.

                  如果 shell 为假,我认为它的行为应该如您所愿.

                  If shell is false, it should behave as you expect, I think.

                  如果您依赖 shellTrue 来使用 PATH 环境变量解析可执行路径,则可以使用 shutil.which 代替,然后通过而是 Popen 的绝对路径.(顺便说一句,如果您使用的是 Python 3.5 或更高版本,则应该使用 subprocess.run 而不是 Popen.

                  If you were relying on shell being True for resolving executable paths using the PATH environment variable, you can accomplish the same thing using shutil.which instead, then pass the absolute path to Popen instead. (As an aside, if you are using Python 3.5 or newer, you should be using subprocess.run rather than Popen.

                  这篇关于使用 Popen 打开进程并获取 PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Pythonic and efficient way of finding adjacent cells in grid(在网格中查找相邻单元格的 Pythonic 和有效方法)
                  map a hexagonal grid in matplotlib(在 matplotlib 中映射六边形网格)
                  Execute arbitrary python code remotely - can it be done?(远程执行任意 python 代码 - 可以吗?)
                  Python - Plotting colored grid based on values(Python - 根据值绘制彩色网格)
                  Is there a GUI design app for the Tkinter / grid geometry?(是否有 Tkinter/网格几何图形的 GUI 设计应用程序?)
                  tkinter Canvas Scrollbar with Grid?(带有网格的 tkinter 画布滚动条?)
                  <i id='wOGT7'><tr id='wOGT7'><dt id='wOGT7'><q id='wOGT7'><span id='wOGT7'><b id='wOGT7'><form id='wOGT7'><ins id='wOGT7'></ins><ul id='wOGT7'></ul><sub id='wOGT7'></sub></form><legend id='wOGT7'></legend><bdo id='wOGT7'><pre id='wOGT7'><center id='wOGT7'></center></pre></bdo></b><th id='wOGT7'></th></span></q></dt></tr></i><div id='wOGT7'><tfoot id='wOGT7'></tfoot><dl id='wOGT7'><fieldset id='wOGT7'></fieldset></dl></div>
                • <small id='wOGT7'></small><noframes id='wOGT7'>

                  <tfoot id='wOGT7'></tfoot>
                      <tbody id='wOGT7'></tbody>
                      <bdo id='wOGT7'></bdo><ul id='wOGT7'></ul>
                          <legend id='wOGT7'><style id='wOGT7'><dir id='wOGT7'><q id='wOGT7'></q></dir></style></legend>