separate process

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 36
Reputation: Dixtosa is an unknown quantity at this point 
Solved Threads: 1
Dixtosa Dixtosa is offline Offline
Light Poster

separate process

 
-2
  #1
Oct 1st, 2009
Hi all.


i want to make separate process with python.

for example i have python code. for example:
  1. import time
  2. time.sleep(5)
  3. print "Time Over"
named: t.py

and i want to do all ^^that^^ just on other process
i hope you understand.


AND DONT GIVE ME LINKS PLS. I GOOGLED THAT MUCH.
BUT NOTHING HELPFUL,
just WRITE CODE WHICH WOULD BE HELPFUL FOR ME!
sorry for My english
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 965
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 222
Gribouillis's Avatar
Gribouillis Gribouillis is offline Offline
Posting Shark

Re: separate process

 
1
  #2
Oct 1st, 2009
One way to do it is to use the subprocess module
  1. import subprocess as SP
  2. child_process = SP.Popen(["python", "t.py"])
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 408
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: separate process

 
0
  #3
Oct 1st, 2009
Not sure what your asking?

Do you mean you want to create:

1. a thread which exists in the current process
2. a new process which exists independently
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 108
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 12
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster

Re: separate process

 
0
  #4
Oct 1st, 2009
you're not making much sense... Do you want to have that processes running on another thread so you can continue doing something else? Do you just want to execute that little script then after it finishes it moves on? What are you trying to do EXACTLY?
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: Dixtosa is an unknown quantity at this point 
Solved Threads: 1
Dixtosa Dixtosa is offline Offline
Light Poster

Re: separate process

 
0
  #5
Oct 1st, 2009
Gribouillis

and where can i donwload thatmodule?

gerard4143
2


ov3rcl0ck
I want to have that processes running on another thread and ability to continue doing something else.
and
I DONT want just to execute that little script then after it finishes it moves on.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: Dixtosa is an unknown quantity at this point 
Solved Threads: 1
Dixtosa Dixtosa is offline Offline
Light Poster

Re: separate process

 
0
  #6
Oct 1st, 2009
ov3rcl0ck, for example, i have code:


...commands1...
...commands2...
call to t.py and imediately go on next command3
...commands3...
...commands4...

and i think separate process is the best in this time
Last edited by Dixtosa; Oct 1st, 2009 at 4:29 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 108
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 12
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster

Re: separate process

 
0
  #7
Oct 1st, 2009
Originally Posted by Dixtosa View Post
Gribouillis

and where can i donwload thatmodule?

gerard4143
2


ov3rcl0ck
I want to have that processes running on another thread and ability to continue doing something else.
and
I DONT want just to execute that little script then after it finishes it moves on.
Originally Posted by Dixtosa View Post
ov3rcl0ck, for example, i have code:


...commands1...
...commands2...
call to t.py and imediately go on next command3
...commands3...
...commands4...

and i think separate process is the best in this time
First off Subprocess is a default module for python, next subprocess doesn't spawn a new thread, you need the multithreading module that comes default with the interpreter, also knowing object orientated programming will help. Try this little example:

  1. import threading
  2.  
  3. class THREAD(threading.Thread):
  4. def __init__(self,ARG1,ARG2):
  5. #If you change the arugments of __init__ modify the bellow also
  6. self.arg1=ARG1
  7. self.arg2=ARG2
  8. def run(self):
  9. #THIS IS WERE YOUR CODE GOES!
  10.  
  11. t=THREAD(ARG1,ARG2)
  12. t.start()
  13. t.join()

The easiest way to do it is make a thread out of a function but this will lose alot of functionality and is a messy way of doing things IMO. Here it is:
  1. import threading,thread
  2.  
  3. def THREAD(ARG1,ARG2):
  4. #YOUR CODE
  5. thread.start_new_thread(THREAD, (ARG1,ARG2))
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: Dixtosa is an unknown quantity at this point 
Solved Threads: 1
Dixtosa Dixtosa is offline Offline
Light Poster

Re: separate process

 
0
  #8
Oct 2nd, 2009
code:
  1. import threading
  2.  
  3. class THREAD(threading.Thread):
  4. def __init__(self,ARG1,ARG2):
  5. #If you change the arugments of __init__ modify the bellow also
  6. self.arg1=ARG1
  7. self.arg2=ARG2
  8. def run(self):
  9. print "RUN"
  10. #THIS IS WERE YOUR CODE GOES!
  11. ARG1,ARG2=1,2
  12.  
  13. t=THREAD(ARG1,ARG2)
  14. t.start()
  15. t.join()

error:
Traceback (most recent call last):
File "D:\Desktop_MARI\threading\th.py", line 14, in <module>
t.start()
File "C:\Python25\lib\threading.py", line 432, in start
raise RuntimeError("thread.__init__() not called")
RuntimeError: thread.__init__() not called
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 108
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 12
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster

Re: separate process

 
0
  #9
Oct 2nd, 2009
  1. import threading
  2.  
  3. class THREAD(threading.Thread):
  4. def __init__(self,ARG1,ARG2):
  5. threading.Thread.__init__(self)
  6. #If you change the arugments of __init__ modify the bellow also
  7. self.arg1=ARG1
  8. self.arg2=ARG2
  9. def run(self):
  10. print "RUN"
  11. #THIS IS WERE YOUR CODE GOES!
  12. ARG1,ARG2=1,2
  13.  
  14. t=THREAD(ARG1,ARG2)
  15. t.start()
  16. t.join()

Forgot a line. But you do realize this is an example and i posted it so you can adjust it to your needs, if you don't understand it you're not ready for threading, so this script probably won't work, and if it does its not gonna do anything.If you don't understand it you're not ready for threading.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: Dixtosa is an unknown quantity at this point 
Solved Threads: 1
Dixtosa Dixtosa is offline Offline
Light Poster

Re: separate process

 
0
  #10
Oct 2nd, 2009
overclock, that isnot what i need.however,thank you .

Gribouillis, that was usefull. however i want to know how to multiproceess. i dont believe that it isnt possible.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC