How do I create a subprocess in Python?

后端 未结 6 1548
孤独总比滥情好
孤独总比滥情好 2021-01-18 09:21

I would like to create a subprocess of a process.

What would be a working example which shows how to accomplish this?

6条回答
  •  一整个雨季
    2021-01-18 10:11

    Launching and monitoring a subprocess:

    import subprocess, time, os, signal
    args=['/usr/bin/vmstat','-n','2']
    app=subprocess.Popen(args=args, stdout=open('somefile','w'))
    print "Your app's PID is %s. You can now process data..." % app.pid
    time.sleep(5)
    if app.poll() == None: print "Process is still running after 5s."
    print "The app outputed %s bytes." % len(open('somefile','r').read())
    print "Stopping the process..."
    os.kill(app.pid, signal.SIGTERM)
    

    There is more to it. Just check the Popen docs.

提交回复
热议问题