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

Recommended Answers

All 5 Replies

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?

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.

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.

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.