Darren Sam Darren Sam - 11 months ago 64
Python Question

Running two Python 2.7 scripts simultaneously

I have two Python scripts, one of which is

try.py
which contains this:

import os

os.system('python hello.py')

print "over"


This script calls another script by name
hello.py
, and
hello.py
contains:

import time

var = 1

while (var < 60):
var = var + 1
print var
time.sleep(0.5)


There are two problems which I am facing:


  • One is I want to run both the scripts simultaneously.

    but in my case my
    try.py
    script continues to run after
    hello.py
    is finished running.


Answer

You might want to use subprocess.Popen():

import subprocess

p = subprocess.Popen(['python', 'hello.py'])

print "over"
p.wait()