stop thread from outside 'run'

Discussion in 'Python' started by Prashant, Jun 5, 2012.

  1. Prashant

    Prashant Guest

    Hi,

    I am using tornado web socket server to communicate between python(server) and browser(client). To mimic the behavior of sending data to client and get results back, I am using threaded approach:

    class JobThread(threading.Thread):
    """"""
    def __init__(self, target):
    """"""
    super(JobThread, self).__init__()
    self.target = target
    self.res = None
    self.stoprequest = threading.Event()
    self.start()
    self.join()
    # Execution stops here and wait
    return self.result()

    def run(self):
    """ Start this thread """
    self.target()
    while not self.stoprequest.isSet():
    print "running..."

    def result(self):
    """"""
    return self.res

    def kill(self, result):
    """ Kill this thread """
    self.res = result
    self.stoprequest.set()

    def ReceiveData(message):
    """"""
    global t
    print str(message)
    t.kill(message)

    def SendData(data_string):
    """"""
    sendMessageWS(data_string)

    result = JobThread(partial(SendData, data_string))

    As soon as jobThread instance starts it sends data to websocket and wait for client to response. Client is sending back results but 'ReceiveData' is not getting called because of infinite loop in 'run' method. The only way you can stop the thread is from outside when "ReceiveData" executes and kill the threaded instance 't'.

    Any pointers?
    Prashant, Jun 5, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Son KwonNam
    Replies:
    11
    Views:
    2,582
    mr_organic
    Apr 9, 2004
  2. Replies:
    3
    Views:
    2,030
    Roland
    Jun 16, 2005
  3. Benji
    Replies:
    34
    Views:
    1,161
    pkriens
    Oct 28, 2005
  4. Krzysztof Poc

    outside type, outside function

    Krzysztof Poc, Feb 3, 2012, in forum: C++
    Replies:
    1
    Views:
    272
    Victor Bazarov
    Feb 7, 2012
  5. Earl T
    Replies:
    2
    Views:
    107
    Earl T
    Jan 13, 2004
Loading...

Share This Page