[Tutor] how to make a script do two things at once.
I-McTaggart, Peter
peter.mctaggart at boeing.com
Mon Aug 22 05:28:38 CEST 2005
You might also try the following from the os module. (taken from the
Python manuals.)
This may be easier than getting your head around threads.
-----------------------------
spawnl( mode, path, ...)
spawnle( mode, path, ..., env)
spawnlp( mode, file, ...)
spawnlpe( mode, file, ..., env)
spawnv( mode, path, args)
spawnve( mode, path, args, env)
spawnvp( mode, file, args)
spawnvpe( mode, file, args, env)
Execute the program path in a new process. If mode is P_NOWAIT, this
function returns the process ID of the new process; if mode is P_WAIT,
returns the process's exit code if it exits normally, or -signal, where
signal is the signal that killed the process. On Windows, the process ID
will actually be the process handle, so can be used with the waitpid()
function.
[...snip...]
As an example, the following calls to spawnlp() and spawnvpe() are
equivalent:
import os
os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')
L = ['cp', 'index.html', '/dev/null']
os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
Availability: Unix, Windows. spawnlp(), spawnlpe(), spawnvp() and
spawnvpe() are not available on Windows. New in version 1.6.
> -----Original Message-----
> From: nephish [mailto:nephish at xit.net]
> Sent: 22 August 2005 1:23
> To: Kent Johnson
> Cc: tutor at python.org
> Subject: Re: [Tutor] how to make a script do two things at once.
>
>
> Kent Johnson wrote:
>
> >nephish wrote:
> >
> >
> >>Hey there,
> >>i have a simple question about getting a script to do
> >>two things at once.
> >>like this.
> >>
> >>
> >>for i in range(100):
> >> print i
> >> time.sleep(.2)
> >> if i == 15:
> >> os.system('python /home/me/ipupdate.py')
> >>
> >>print 'done'
> >>
> >>when i run this, it stops at 15 and runs the script called
> out in the
> >>os.system line. i know it is supposed to do that. But, how
> could i get a
> >>script to do this without stopping the count (or delaying
> it unill the
> >>script called exits)
> >>
> >>
> >
> >One way to get a script to do two things 'at once' is to use
> threads.
> >Threads are also a good way to introduce strange bugs into
> your program
> >so you should do some reading about them. I can't find a good
> >introduction - anyone else have a suggestion? Here is a brief one:
> >http://www.wellho.net/solutions/python-python-threads-a-first
-example.h
>tml
>
>Here are a couple of articles, not really introductory:
>http://linuxgazette.net/107/pai.html
>http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/
>
>Anyway here is something to get you started, this version of your
>program starts a new thread to do the os.system call, that way the main
>thread doesn't block.
>
>import os, threading, time
>
>def doSomething():
> ''' This function will be called from the second thread '''
> os.system('''python -c "from time import sleep;sleep(2);print
>'hello'"''')
>
>for i in range(30):
> print i
> time.sleep(.2)
> if i == 10:
> print 'Starting thread'
> threading.Thread(target=doSomething).start()
>
>print 'done'
>
>Kent
>
>_______________________________________________
>Tutor maillist - Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>
>
thanks for all of the responses, yep, looks like threads is what i want
to go with. got the docs you guys linked me to bookmarked. this is going
to take a bit of research.
thanks again for showing me where to start.
shawn
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list