[Tutor] Threading in Python

STAN stan@coolquiz.com
Thu, 21 Mar 2002 21:43:20 -0800 (PST)


Hi,


How is it possible to run a seperate process in the background using Python ?


For Eg:

I have a main process that is console based, that gives the user a few options and acts according to their choice.

But I want a process to be running in the background, which sleeps for x secs and keeps checking for some data [ Contacts a C++ extension].

I tried thread.start_new_thread(...) and also the Thread class but was unsuccessful :

This is what I tried ( a very simple and abstract simulation of what I intend to do)::

*****************************************************
from threading import *
from random import Random


def printResult():
	r = Random(68)
	while 1:
		time.sleep(0.5)   # Is there a wait method I can use here for more efficiency ?
		x = r.random()
		if (x > 0.75):
			print "The Event has occured"

# End of printResult()


def startit():
	t = Thread(target = printResult(),args=())
	t.start()
	while 1:
		ch = raw_input("Continue ?")
		if (ch not in ('y',)):
			break

# End of startit()

>>> startit()

*************************************************************************************

I wanted printResult to be running in the background ..... But it is the only one that is running !!!

Could anybody suggest me as to how I can use threads effectively in Python to acheive my task.

Thanks and Regards
  S

_____________________________________________________________
Cool Quiz - Websites That Keep You Guessing! http://www.coolquiz.com

_____________________________________________________________
Run a small business? Then you need professional email like you@yourbiz.com from Everyone.net  http://www.everyone.net?tag