1

I have updated the question to be more clear I want to execute function while printing numbers in background and check the condition.

import time

number = [1,100]
t0 = time.time()
for i in number:
    print i
t1= time.time()


def sum_two_numbers():
   t2 = time.time()
   c=1+2
   t3 =time.time()


verify t0<t2 and t3<<t1
rgerts
  • 71
  • 1
  • 7
  • With some python? What have you tried? What have you searched for? Have you tried looking up Threading in the Python Standard Library documentation? – DisappointedByUnaccountableMod Sep 02 '15 at 13:02
  • I am unknown to python subprocess and threading, i don't know which will solve the purpose – rgerts Sep 02 '15 at 13:08
  • So was I before I tried using them. Try using Threading. Look at the examples, search for examples, ... it's all part of learning. – DisappointedByUnaccountableMod Sep 02 '15 at 13:15
  • @AbhishekSharma - Barny is right. You will require both. subprocess to execute the script and threading for simultaneous operation. For threading - http://stackoverflow.com/questions/2846653/python-multithreading-for-dummies . For subprocess - http://stackoverflow.com/questions/89228/calling-an-external-command-in-python – Dinesh Pundkar Sep 02 '15 at 13:17
  • Thanks for the pointer. – rgerts Sep 02 '15 at 13:17
  • @DineshPundkar: threads are not required. `subprocess.Popen()` will run child processes in the background if it does not interact with the child's output. The parent Python code will continue executing, possibly starting new child processes. – mhawke Sep 02 '15 at 13:39
  • @mhawke - Thanks for catch..!!! Cheers !!!! – Dinesh Pundkar Sep 02 '15 at 13:41
  • Updated the question – rgerts Sep 02 '15 at 15:11
  • That's quite a substantial change to your question. You now seem to have only one script and you want to run some code concurrently with a function. That suggests threads or multiprocessing modules. – mhawke Sep 02 '15 at 22:12
  • Yes as the script two is just doing addition, I thought of adding it to same script. – rgerts Sep 04 '15 at 06:27

1 Answers1

1

As the two scripts are completely independent, just use subprocess.Popen():

import subprocess

script1 = subprocess.Popen(['/path/to/script1', 'arg1', 'arg2', 'etc'])
script2 = subprocess.Popen(['/path/to/script2', 'arg1', 'arg2', 'etc'])

That's it, both scripts are running in the background1. If you want to wait for one of them to complete, call script1.wait() or script2.wait() as appropriate. Example:

import subprocess

script1 = subprocess.Popen(['sleep', '30'])
script2 = subprocess.Popen(['ls', '-l'])
script1.wait()

You will find that script 2 will produce its output and terminate before script 1.

If you need to capture the output of either of the child processes then you will need to use pipes, and then things get more complicated.

1 Here "background" is distinct from the usual *nix notion of a background process running in a shell; there is no job control for example. WRT subprocess, a new child process is simply being created and the requested executable loaded. No shell is involved, provided that shell=False as per the default Popen() option.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • Hi mhawke, two scripts are independent of each other, I want to run script 1 in background and execute script 2 while script 1 is already running. – rgerts Sep 02 '15 at 13:17
  • OK. You should update your _question_ with that somewhat important information. – mhawke Sep 02 '15 at 13:23
  • @AbhishekSharma : answer updated according to your comment. – mhawke Sep 02 '15 at 13:59