pythonic exec* spawn*
Nick Craig-Wood
nick at craig-wood.com
Mon Feb 13 06:30:04 EST 2006
Daniel Nogradi <nogradi at 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/module-subprocess.html
You'll probably find threading easier though.
http://www.python.org/doc/2.4.2/lib/module-threading.html
But it will use your multiple CPUs less efficiently than fork()-ing.
--
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list