954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

separate process

Hi all.


i want to make separate process with python.

for example i have python code. for example:

import time
time.sleep(5)
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 :)

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

One way to do it is to use the subprocess module

import subprocess as SP
child_process = SP.Popen(["python", "t.py"])
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

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?

ov3rcl0ck
Junior Poster
113 posts since Sep 2009
Reputation Points: 35
Solved Threads: 22
 

Gribouillis

and where can i donwload thatmodule?

gerard4143
2:D


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. :)

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

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 :)

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

Gribouillis

and where can i donwload thatmodule?

gerard4143 2:D

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. :)

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:

import threading

class THREAD(threading.Thread):
	def __init__(self,ARG1,ARG2):
		#If you change the arugments of __init__ modify the bellow also
		self.arg1=ARG1
		self.arg2=ARG2
	def run(self):
		#THIS IS WERE YOUR CODE GOES!

t=THREAD(ARG1,ARG2)
t.start()
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:

import threading,thread

def THREAD(ARG1,ARG2):
	#YOUR CODE
thread.start_new_thread(THREAD, (ARG1,ARG2))
ov3rcl0ck
Junior Poster
113 posts since Sep 2009
Reputation Points: 35
Solved Threads: 22
 

code:

import threading
 
class THREAD(threading.Thread):
	def __init__(self,ARG1,ARG2):
		#If you change the arugments of __init__ modify the bellow also
		self.arg1=ARG1
		self.arg2=ARG2
	def run(self):
		print "RUN"
		#THIS IS WERE YOUR CODE GOES!
ARG1,ARG2=1,2

t=THREAD(ARG1,ARG2)
t.start()
t.join()


error:
Traceback (most recent call last):
File "D:\Desktop_MARI\threading\th.py", line 14, in
t.start()
File "C:\Python25\lib\threading.py", line 432, in start
raise RuntimeError("thread.__init__() not called")
RuntimeError: thread.__init__() not called

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 
import threading
 
class THREAD(threading.Thread):
	def __init__(self,ARG1,ARG2):
		threading.Thread.__init__(self)
		#If you change the arugments of __init__ modify the bellow also
		self.arg1=ARG1
		self.arg2=ARG2
	def run(self):
		print "RUN"
		#THIS IS WERE YOUR CODE GOES!
ARG1,ARG2=1,2

t=THREAD(ARG1,ARG2)
t.start()
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.

ov3rcl0ck
Junior Poster
113 posts since Sep 2009
Reputation Points: 35
Solved Threads: 22
 

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.

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

Multi processing is similar to threading, google "python multiprocess"

ov3rcl0ck
Junior Poster
113 posts since Sep 2009
Reputation Points: 35
Solved Threads: 22
 

Multiprocessing and threading are fairly similar, in fact multiprocessing is based off the multithreading API. Heres a really really easy example:

from multiprocessing import Process

def f(name):
    print 'hello', name

if __name__ == '__main__':
    p = Process(target=f, args=('bob',)) #inits thread
    p.start() #starts thread
    p.join() #waits untill thread is done
ov3rcl0ck
Junior Poster
113 posts since Sep 2009
Reputation Points: 35
Solved Threads: 22
 

and where can i donwload multiprocessing.py?

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

multiprossessing is a standard module for python >= 2.6. For older versions, you must download pyprocessing.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

i downloaded pyprocess. installed an=d got error:
'module' object has no attribute 'f'

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 
i downloaded pyprocess. installed an=d got error: 'module' object has no attribute 'f'


post your code

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

gribilus, overclock's code:

from multiprocessing import Process
 
def f(name):
    print 'hello', name
 
if __name__ == '__main__':
    p = Process(target=f, args=('bob',)) #inits thread
    p.start() #starts thread
    p.join() #waits untill thread is done
Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

gribilus, overclock's code:

from multiprocessing import Process
 
def f(name):
    print 'hello', name
 
if __name__ == '__main__':
    p = Process(target=f, args=('bob',)) #inits thread
    p.start() #starts thread
    p.join() #waits untill thread is done


On my machine it works well with python 2.6.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

i have 2.5

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

2.6 has many bugs so i prefer 2.5

Dixtosa
Light Poster
38 posts since Jul 2009
Reputation Points: 9
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You