473,387 Members | 1,766 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,387 software developers and data experts.

pythonic exec* spawn*

Is it possible to pass a python object to a python program as argument?

In my program I would like to start executing an other python program
and don't wait until that finishes, only launch it and keep running
the original program. The obvious way I can think of it is using the
exec* or spawn* function families. However, these are generic
functions to start any external program and I was wondering if there
was a special way to start a python program (as opposed to an
arbitrary program), especially because I would like to pass data to
this other program in the form of python objects.

While the original program is running it creates all sorts of data in
a nice pythonic way, for example as a class instance with lot of
member data, and then I wouldn't want to convert all of this into
command line arguments because that would create a huge list. I would
like to pass the whole object at once to the second python program
which should start doing its thing with it while the original program
should keep running.

I was looking around for threading and forking but couldn't come up
with a clear solution.
Feb 9 '06 #1
9 1649
You could possibly pickle an object and send it, but that could be a
bit messy...

Feb 9 '06 #2
you can use the threads commands

Feb 9 '06 #3
km****@gmail.com wrote:
You could possibly pickle an object and send it, but that could be a
bit messy...

pickle it (or shelve?) and then pass the pickle/shleve filename to the
other process as a command line parameter?

Not sure if this would work or not, but that's where I would start.

-Don
Feb 9 '06 #4
Daniel Nogradi:
I would like to pass the whole object at once to the second python
program which should start doing its thing with it while the original
program should keep running.


os.fork() does that (on Mac and Unix).

--
René Pijlman
Feb 9 '06 #5
> >I would like to pass the whole object at once to the second python
program which should start doing its thing with it while the original
program should keep running.


os.fork() does that (on Mac and Unix).


Okay, but how? It seems to me that if the process which issued
os.fork() ends, then the forked process also ends. And what I would
need is that regardless of the execution time of the original program,
the second one which the original starts (or forks) should finish what
it supposed to do. Roughly I have the following in mind:

-------------------------------------------------------------------------------
def longer( data ):
# do some stuff, that takes long
# do something to collect data

data = something

# here call the function longer( data ) in a way
# that execution of the code doesn't stop, so
# we get to the following print and then to the end

print "okay, the first program finished"
------------------------------------------------------------------------------

But the execute of longer( data ) should keep going even though the
original program ended. I'm pretty sure it's something basic and
probably I'm not aware of the right concepts and that's why I can't
find the right place to look in the docs.
Feb 10 '06 #6
Daniel Nogradi:
os.fork() does that (on Mac and Unix).
Okay, but how?


Sorry, fork() is implemented strictly on a 'need to know' basis :-)
It seems to me that if the process which issued os.fork() ends, then
the forked process also ends.
No, no, they're not a quantum mechanic photon pair. Every process decides
for itself if and when to end. As long as it's not signalled/killed, that
is.
But the execute of longer( data ) should keep going even though the
original program ended. I'm pretty sure it's something basic and
probably I'm not aware of the right concepts and that's why I can't
find the right place to look in the docs.


You can search for "daemonize".

Here's a recipe:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

The concept is best explained in this book:
http://www.kohala.com/start/apue.html

--
René Pijlman
Feb 10 '06 #7
> >> os.fork() does that (on Mac and Unix).

Okay, but how?


Sorry, fork() is implemented strictly on a 'need to know' basis :-)
It seems to me that if the process which issued os.fork() ends, then
the forked process also ends.


No, no, they're not a quantum mechanic photon pair. Every process decides
for itself if and when to end. As long as it's not signalled/killed, that
is.
But the execute of longer( data ) should keep going even though the
original program ended. I'm pretty sure it's something basic and
probably I'm not aware of the right concepts and that's why I can't
find the right place to look in the docs.


You can search for "daemonize".

Here's a recipe:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

The concept is best explained in this book:
http://www.kohala.com/start/apue.html


Okay, thanks a lot, I'll look into that.
Feb 10 '06 #8
> > >> os.fork() does that (on Mac and Unix).

Okay, but how?


Sorry, fork() is implemented strictly on a 'need to know' basis :-)
It seems to me that if the process which issued os.fork() ends, then
the forked process also ends.


No, no, they're not a quantum mechanic photon pair. Every process decides
for itself if and when to end. As long as it's not signalled/killed, that
is.
But the execute of longer( data ) should keep going even though the
original program ended. I'm pretty sure it's something basic and
probably I'm not aware of the right concepts and that's why I can't
find the right place to look in the docs.


You can search for "daemonize".

Here's a recipe:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/66012

The concept is best explained in this book:
http://www.kohala.com/start/apue.html


Thanks again, the recipe was very useful, and in the meantime I also
found this article,
http://www.informit.com/content/imag...rk_article.pdf
which explains os.fork and os.wait in a python context. Maybe some
others will also make use of it, it clearified the basics for me.
Feb 11 '06 #9
Daniel Nogradi <no*****@gmail.com> wrote:
Is it possible to pass a python object to a python program as
argument?
Yes - you can pickle it.

http://www.python.org/doc/2.4.2/lib/module-cPickle.html

You can pickle just about anything into a string and unpickle it back
into python objects. You shouldn't accept pickles from untrusted
sources as this may reperesent a security problem.
In my program I would like to start executing an other python program
and don't wait until that finishes, only launch it and keep running
the original program. The obvious way I can think of it is using the
exec* or spawn* function families. However, these are generic
functions to start any external program and I was wondering if there
was a special way to start a python program (as opposed to an
arbitrary program), especially because I would like to pass data to
this other program in the form of python objects.

While the original program is running it creates all sorts of data in
a nice pythonic way, for example as a class instance with lot of
member data, and then I wouldn't want to convert all of this into
command line arguments because that would create a huge list. I would
like to pass the whole object at once to the second python program
which should start doing its thing with it while the original program
should keep running.

I was looking around for threading and forking but couldn't come up
with a clear solution.


If you want to fork(), then I would use the subprocess module to
create a child process with a pipe to it. I would then pass python
objects as pickles back and forth across that pipe.

http://www.python.org/doc/2.4.2/lib/...ubprocess.html

You'll probably find threading easier though.

http://www.python.org/doc/2.4.2/lib/...threading.html

But it will use your multiple CPUs less efficiently than fork()-ing.

--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick
Feb 13 '06 #10

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

Similar topics

4
by: Benoit Dejean | last post by:
hello, i have a question about forking processes atm, i have some code which i want to rewrite os.system("cd ~ && exec " + cmd + " & disown") i want to remove this os.system call
3
by: Lingyun Yang | last post by:
Hi, I want to use python as a "shell like" program, and execute an external program in it( such as mv, cp, tar, gnuplot) I tried: os.execv("/bin/bash",("/usr/bin/gnuplot",'-c "gnuplot <...
3
by: David MacKay | last post by:
Dear Greater Py, <motivation note="reading this bit is optional"> I am writing a command-line reader for python. I'm trying to write something with the same brevity as perl's one-liner ...
4
by: Shailesh Humbad | last post by:
I was trying to exec a background process on XP using PHP CLI, but could not get it to work. Suppose the command I want to spawn off is "cmd". On *nix, it is as easy as putting ampersand "&" at...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
3
by: jeremyfee | last post by:
The spawn* and exec* functions appear to escape asterisks, I'm guessing all shell characters too, before the spawn/exec'ed process sees them. Is there a way around this? Not sure if this is a...
2
by: AndrewTK | last post by:
Hi, I am trying to write a Python script that takes a ZIP file from a web form (using CGI) and uses either of the UN*X unzip, gunzip, tar, bunzip2 utilities to expand it. I can use Python to...
4
by: Brendan Miller | last post by:
I want to spawn a child process based on an external executable that I have the path for. I then want to wait on that executable, and capture it's output. In the os module, fork is only...
0
by: Jean-Paul Calderone | last post by:
On Fri, 02 May 2008 06:30:03 -0500, Nick Craig-Wood <nick@craig-wood.comwrote: I haven't read all of this thread, so excuse me if this is out of place. There is a good cross-platform solution,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.