Re: how to handle threads with webkit

Christoph Zwerschke <[email protected]>
Newsgroups gmane.comp.python.webware
Message-ID <[email protected]>
Salman Toor wrote:
> What is the proper way of using multi threaded application within WebKit.
> 
> for example:
> 
> # define a class that subclasses Thread
> class showTime(threading.Thread):
> 
>   # define instance constructor
>   def __init__(self,interval,id):
>      self.w = interval
>      self.id = id
>      threading.Thread.__init__(self)  # we are required to this
> 
>   # define run method (body of the thread)
>   def run(self):
>      time.sleep(self.w)
>      print "thread", self.id, "done at", time.ctime(time.time())
>  
> how do i start the threads so that it can finish successfully and i
> can shutdown my server and can restart it.

Hi Salman,

here is a simple example servlet. The thread is started when the servlet 
is first called and stopped when the AppServer stops.

---------------------------------------------------------------

from ExamplePage import ExamplePage
from time import *

from threading import Thread, Event

class MyThread(Thread):

     def __init__(self, interval, id):
         Thread.__init__(self)
         self.interval = interval
         self.id = id
         self.time = None
         self.stop_event = Event()

     def run(self):
         while not self.stop_event.isSet():
             self.time = ctime(time())
             print "thread", self.id, "done at", self.time
             self.stop_event.wait(self.interval)

     def stop(self):
         print "thread", self.id, "shutdown"
         self.stop_event.set()
         self.join(1)

my_thread = MyThread(2, 4711)
from WebKit.AppServer import globalAppServer
globalAppServer.application().addShutDownHandler(my_thread.stop)
my_thread.start()

class ShowTime(ExamplePage):

     def writeContent(self):
         self.write('<h1>Thread Test</h1>')
         self.write('<h2>Last executed at: ', my_thread.time, '</h2>')


---------------------------------------------------------------

Hope that helps.

-- Christoph

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.