Scenario
I am invoking python scripts using exec(...) in php, and python works its part and it is unstructured environment, there is no django or any other framework.
And a product is client based product & most of the tasks are long running tasks.
So if there is any request from client, it must be satisfied immediately. Here for us, time is also a main concern we can not delay the process.
So if I will get too much requests from client (May be, crons are also working at the same time), I will get too much python processes and according to my knowledge each exec(...) will invoke interpreter. So if anyhow I can prevent invoking the interpreter at each call, it will be good for me. Memory space of 1Kb or 1Mb is very useful.
Problems
- Memory usage is high (If I can save 1Mb, I want try any solution. Assuming script is well written. Proper sleep is given in looping and garbage collector is managed well.)
- Sometimes for long running python script, if that script is invoked from php, using exec(...) and process is not invoked in background, and that python script is over, and still it takes time to execute the next line after that exec.
Solution
For the solution, I thought I should use API calls for python. But remember I can not use any framework. To use framework I need to rewrite the code, and that is not possible in my case. So CGI and FCGI can help me here.
To run python scripts from API calls, I need to enable CGI to run.
But what I found is interesting ::
Programs using CGI, to communicate with their web server, need to be started by the server for every request. So, every request starts a new Python interpreter – which takes some time to start up – thus making the whole interface only usable for low load situations.
If I will run a script like following, than all will start at once
subprocess.Popen(["python", "program1.py"])
subprocess.Popen(["python", "program2.py"])
subprocess.Popen(["python", "program3.py"])
subprocess.Popen(["python", "program4.py"])
subprocess.Popen(["python", "program5.py"])
subprocess.Popen(["python", "program6.py"])
Assume this all programs are long running task.
1) Can I prevent python module from creating more interpreter and if yes how can I?
2) How many maximum interpreters, a python module can create (at once like the above code) ?