Re: [Tutor] how to make a script do two things at once.
On Sun, 21 Aug 2005 16:23:20 -0500 nephish [EMAIL PROTECTED] 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) I don' t have to run it this way, i can import it if necessary as a module. or whatever will work so i can execute two things at once. If you just need to call a unix system command you can simply add to the command string to make it run in the background. Regards Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to make a script do two things at once.
Michael Lange wrote: On Sun, 21 Aug 2005 16:23:20 -0500 nephish [EMAIL PROTECTED] 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) I don' t have to run it this way, i can import it if necessary as a module. or whatever will work so i can execute two things at once. If you just need to call a unix system command you can simply add to the command string to make it run in the background. Regards Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Well Cool , yeah, i run linux. this would work out great. thanks! shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to make a script do two things at once.
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) I don' t have to run it this way, i can import it if necessary as a module. or whatever will work so i can execute two things at once. thanks shawn ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to make a script do two things at once.
On Sun, 21 Aug 2005, nephish wrote: i have a simple question about getting a script to do two things at once. Hi Shawn, It sounds like you may want to try threading. Here you go: http://www.python.org/doc/lib/module-threading.html Aahz has written a tutorial about Threads here: http://starship.python.net/crew/aahz/OSCON2001/ but if you'd like to see more examples, please feel free to ask, and people on the tutor list can help. Good luck! ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to make a script do two things at once.
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.html 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 - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
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.html 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 - [EMAIL PROTECTED] 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 - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to make a script do two things at once.
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:[EMAIL PROTECTED] Sent: 22 August 2005 1:23 To: Kent Johnson Cc: [EMAIL PROTECTED] 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 - [EMAIL PROTECTED] 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 - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor