0

Hi,

I am hosting a UDP server as part of my application on a thread. The problem is when I close the application it doesn't exit completely because the server is still running on the thread.

class pdu_parser(Thread):


    run_parser = True
    sock = None

    def __init__(self):

        Thread.__init__(self)



    def run(self):

        print 'Parser listening on PORT: ', UDP_PORT

        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        self.sock.bind((UUDP_IP, UDP_PORT))

        while self.run_parser:

            data, addr = self.sock.recvfrom(1024)

            self.parse_lt(data)
	    print '.'
	print 'Parser Stopped'


    def stopParser(self):
	"""
	Stop the thread
	"""
	self.run_parser = False
	self.sock.close()
	print 'making parser false'

I start the above thread in another objects constructor and i am trying to call the stopParser() method in the destructor.

Please help

2
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by jcao219
0

I start the above thread in another objects constructor and i am trying to call the stopParser() method in the destructor.

I'm not understanding you there.
Why can't you call the stopParser method?

0

I'm not understanding you there.
Why can't you call the stopParser method?

That is the problem. Even if i call the stopParser method, it doesnt work.

0
while self.run_parser:

            data, addr = self.sock.recvfrom(1024)

            self.parse_lt(data)
	    print '.'

I think the problem is there.
The while loop never finishes, so it never ends.

unfortunately, cpython doesn't have a thread.abort() method.
This problem really stumps me.

Edited by jcao219: n/a

0

I think the problem is there.
The while loop never finishes, so it never ends.

unfortunately, cpython doesn't have a thread.abort() method.
This problem really stumps me.

The thing that bothers me is that, when i call the stopParser method and change the value of the self.run_parser variable the while loop doesn't end.

Any ideas why is this so?

0

Yeah, here's my idea:

#this is an example
while something:
      a = 1
      a = 2
      del a

When the program gets to that code above, it first checks if "something" is True, then it proceeds to do a = 1 ,
and then a = 2 ,
and then del a ,
after that,
it checks if "something"
is still True, and then it will repeat.

In your code,
the loop gets stuck at self.parse_lt(data) ,
and so it doesn't matter if
"self.run_parser" is False,
it never gets there.

Hopefully that makes sense.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.