Re: how to handle threads with webkit

"Salman Toor" <[email protected]>
Newsgroups gmane.comp.python.webware
Message-ID <[email protected]>
Hi,

Thank you Christoph.
I still have another question. If we are using commands.getoutput() function
inside the run() function then how do we handle the processes. Because this
senario again hangs the Application Server.

Examples:

from ExamplePage import ExamplePage
from time import *
import commands
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())
        result  =  commands.getoutput("pwd")        # if I change this
with result
= "Hello World" the whole program works fine but with this

  command
it again hangs the Application Server. I think it
creates another sub process to run

 commands

            #print "thread", self.id, "done at", self.time
        print result
            #self.stop_event.wait(self.interval)

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

my_thread = []


for i in range(2):
        my_thread.append(MyThread(2, i))


from WebKit.AppServer import globalAppServer

for k in my_thread:
        globalAppServer.application().addShutDownHandler(k.stop)
for j in my_thread:
        j.start()
for k in my_thread:
        k.join()
for j in my_thread:
        j.stop()
class ShowTime(ExamplePage):

    def writeContent(self):
        self.write('<h1>Thread Test</h1>')
        for k in my_thread:
                self.write('<h2>Thread Number 'k' is executing </h2>')

Thanks in Advance.

Best Regards.....
Salman Toor.





On 1/13/07, Christoph Zwerschke <[email protected]> wrote:
> 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
> _______________________________________________
> Webware-discuss mailing list
> Webware-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
> https://lists.sourceforge.net/lists/listinfo/webware-discuss
>

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

_______________________________________________
Webware-discuss mailing list
Webware-discuss-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org
https://lists.sourceforge.net/lists/listinfo/webware-discuss
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.