473,406 Members | 2,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Launching a subprocess without waiting around for the result?

Hi,

I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

What is the correct way to launch subprocess without waiting for the
result to return?

Thanks!
Sep 18 '08 #1
9 6419
erikcw <er***********@gmail.comwrites:
I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.
For "how do I deal with subprocesses from Python", the (new in Python
2.4) 'subprocess' module is the default go-to answer
<URL:http://www.python.org/doc/lib/module-subprocess>, replacing a
rather fragmented set of modules before it.
What is the correct way to launch subprocess without waiting for the
result to return?
Creating an instance of 'subprocess.Popen' will launch the process and
return the Popen instance. You then have the option of polling it or
waiting for it to complete.

--
\ “To stay young requires unceasing cultivation of the ability to |
`\ unlearn old falsehoods.” —Robert Anson Heinlein |
_o__) |
Ben Finney
Sep 18 '08 #2
On Sep 18, 3:33*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
erikcw <erikwickst...@gmail.comwrites:
I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

For "how do I deal with subprocesses from Python", the (new in Python
2.4) 'subprocess' module is the default go-to answer
<URL:http://www.python.org/doc/lib/module-subprocess>, replacing a
rather fragmented set of modules before it.
What is the correct way to launch subprocess without waiting for the
result to return?

Creating an instance of 'subprocess.Popen' will launch the process and
return the Popen instance. You then have the option of polling it or
waiting for it to complete.

--
*\ * * To stay young requires unceasing cultivation of the ability to |
* `\ * * * * * * * * * unlearn old falsehoods. Robert Anson Heinlein |
_o__) * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * *|
Ben Finney
So if I create a Popen object and then just ignore the object and exit
the program the subproccess will finish it's work and then exit itself
cleanly?
Sep 18 '08 #3
erikcw <er***********@gmail.comwrites:
On Sep 18, 3:33*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
erikcw <erikwickst...@gmail.comwrites:
What is the correct way to launch subprocess without waiting for
the result to return?
Creating an instance of 'subprocess.Popen' will launch the process
and return the Popen instance. You then have the option of polling
it or waiting for it to complete.

So if I create a Popen object and then just ignore the object and
exit the program the subproccess will finish it's work and then exit
itself cleanly?
Ah, no, that's a different thing. If the parent exits, the child will
also be killed I believe.

If you want to spawn a process and have it live on independent of the
parent, you want to make the child process a "daemon", detatching
itself from the parent's environment. I don't recall how that's done
immediately, but those are the terms to search for.

--
\ “Pinky, are you pondering what I'm pondering?” “Yes Brain, but |
`\ if our knees bent the other way, how would we ride a bicycle?” |
_o__) —_Pinky and The Brain_ |
Ben Finney
Sep 19 '08 #4
erikcw wrote:
On Sep 18, 3:33*pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
>erikcw <erikwickst...@gmail.comwrites:
I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

For "how do I deal with subprocesses from Python", the (new in Python
2.4) 'subprocess' module is the default go-to answer
<URL:http://www.python.org/doc/lib/module-subprocess>, replacing a
rather fragmented set of modules before it.
What is the correct way to launch subprocess without waiting for the
result to return?

Creating an instance of 'subprocess.Popen' will launch the process and
return the Popen instance. You then have the option of polling it or
waiting for it to complete.

--
\ * * “To stay young requires unceasing cultivation of the ability to |
`\ * * * * * * * * * unlearn old falsehoods.” —Robert Anson Heinlein |
_o__) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *|
Ben Finney

So if I create a Popen object and then just ignore the object and exit
the program the subproccess will finish it's work and then exit itself
cleanly?
Just so happens that I ran into the same problem recently. I started with
exec(), then os.system(), next os.popen(), and last os.spawn().
This is what I discovered on a windows platform. The exec() replaced the
current running process. os.system did not start a completely new process.
os.popen() created a new process but did not open a command box to display
any print statements. Lastly os.spawn() worked - it created a new process
and opened a command box to display any print statements I needed.

Johnf

Sep 19 '08 #5
r0g
erikcw wrote:
Hi,

I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

What is the correct way to launch subprocess without waiting for the
result to return?

Thanks!
Try exec() with " &" at the end of your command line.

Roger.
Sep 19 '08 #6
r0g
erikcw wrote:
Hi,

I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

What is the correct way to launch subprocess without waiting for the
result to return?

Thanks!
Whoops, that was PHP! Imeant...

os.system(yourcommandline+" &")

;-)

Roger
Sep 19 '08 #7
On Fri, Sep 19, 2008 at 10:48 PM, Almar Klein <al*********@gmail.comwrote:
>Ah, no, that's a different thing. If the parent exits, the child will
also be killed I believe.

Not if it's stuck in some endless loop...
>If you want to spawn a process and have it live on independent of the
parent, you want to make the child process a "daemon", detatching
itself from the parent's environment. I don't recall how that's done
immediately, but those are the terms to search for.

I'm curious how this can be done, does anyone know this?

Almar

--
http://mail.python.org/mailman/listinfo/python-list

First result in "making a daemon in python with google":

http://mail.python.org/pipermail/pyt...ry/427692.html

(not tested)

Regards
Marco
--
Marco Bizzarri
http://notenotturne.blogspot.com/
http://iliveinpisa.blogspot.com/
Sep 19 '08 #8
Almar Klein wrote:
>
Ah, no, that's a different thing. If the parent exits, the child will
also be killed I believe.
Not if it's stuck in some endless loop...

If you want to spawn a process and have it live on independent of the
parent, you want to make the child process a "daemon", detatching
itself from the parent's environment. I don't recall how that's done
immediately, but those are the terms to search for.
I'm curious how this can be done, does anyone know this?
I just dove into this several day ago for a small project.

On Linux it's easy -- it involves a couple of forks and other system
calls. Google for daemonize.py.
<http://github.com/lfittl/python-helpers/tree/master/daemonize.py>

On Windows, a bit of searching seems to find a consensus that the way to
do something similar is as a Window's service. I'm just now looking
into how to register and start a service, and how to stop and remove it
later. Google finds lots of information on this -- perhaps I'll post my
result when I've pulled it all together.

Gary Herron
Sep 19 '08 #9
On Sep 18, 5:33 pm, erikcw <erikwickst...@gmail.comwrote:
Hi,

I have a cgi script where users are uploading large files for
processing. I want to launch a subprocess to process the file so the
user doesn't have to wait for the page to load.

What is the correct way to launch subprocess without waiting for the
result to return?

Thanks!
both os.spawn or subprocess can be used. I actually find subprocess
hard to remember so usually prefer os.spawn. For various examples and
explanations, see

http://effbot.org/librarybook/os.htm
Sep 19 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Qiangning Hong | last post by:
I decide to seperate my data collection routine from my data analysis and storage program to a seperate process, so I try to use the new subprocess model in Python 2.4. The main program spawns...
3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
0
by: Christoph Haas | last post by:
Evening, I'm having trouble with running a process through Python 2.4's subprocess module. Example code: ======================================================== def run(command): run =...
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
2
by: Jim | last post by:
Hello, I need a program that will traverse a directory tree to ensure that there are unix-style line endings on every file in that tree that is a text file. To tell text files from others I...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
1
by: WolfgangZ | last post by:
Hello, I'm starting some subprocesses inside a loop. The processes run independent and dont need any communication between each other. Due to memory issues I need to limit the number of running...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.