To Kill a Process that is Accepting Connection on Socket

M

mumebuhi

I removed my previous post about this topic because I apparently have
pasted the wrong code. Sorry for the confusion and thanks for being
patient.

I am having problem to kill the following script completely. The script
basically does the following. The main thread creates a new thread,
which does a completely useless thing, and then starts excepting for a
connection via socket.

# start
import pickle
import signal
import simplejson
import socket
import sys
import threading
import time

counter = 0

class WorkerThread(threading.Thread):
def run(self):
global counter
while True:
counter += 1
time.sleep(10)

worker_thread = WorkerThread()
worker_thread.start()

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 2727))
server.listen(2)

def cleanup(signal, frame):
print "die now"
worker_thread.join(1)
server.shutdown(socket.SHUT_RDWR)
sys.exit(0)
signal.signal(signal.SIGINT, cleanup)

while True:
channel, details = server.accept()
stats = { 'key': "value" }
s = simplejson.dumps(stats)
channel.send(s)
channel.close()
# end

The way I can think of right now is to kill the script using Ctrl+C.
Hence, the signal handler in the code. However, the interpreter
complains:
$ python ex_server.py
die now
Traceback (most recent call last):
File "ex_server.py", line 33, in ?
channel, details = server.accept()
File "/usr/lib/python2.4/socket.py", line 169, in accept
sock, addr = self._sock.accept()
File "ex_server.py", line 28, in cleanup
server.shutdown(socket.SHUT_RDWR)
File "<string>", line 1, in shutdown
socket.error: (128, 'Transport endpoint is not connected')

Does anybody know a better way to do this?

Thank you.

Buhi
 
D

Dennis Lee Bieber

On 27 Oct 2006 06:58:23 -0700, "mumebuhi" <[email protected]> declaimed
the following in comp.lang.python:


makeWork = True
class WorkerThread(threading.Thread):
def run(self):
global counter
while True: while makeWork:
counter += 1
time.sleep(10)

worker_thread = WorkerThread()
worker_thread.start()

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('', 2727))
server.listen(2)
Okay, we have a socket that has nothing talking to it...
def cleanup(signal, frame):
print "die now"
global makeWork
makeWork = False
worker_thread.join(1)
worker_thread.join(11) #one second longer than the worker's sleep

Well... Let's see, you wait up to 1 second for the thread to exit --
but the simple thread you have above will never exit... .join() will not
kill a thread, it only waits for a thread to finish on its own.
server.shutdown(socket.SHUT_RDWR)

Now you shutdown all I/O on the socket.
sys.exit(0)
signal.signal(signal.SIGINT, cleanup)
I've not used signal module... and as I recall, delivery of <ctrl-c>
(I'm on Windows) tends to be somewhat indeterminate, it could go to the
thread or the main function.
while True:
channel, details = server.accept()

Ah, finally an accept() call... But who is connecting?
stats = { 'key': "value" }
s = simplejson.dumps(stats)
channel.send(s)
channel.close()

And here we close the connection...
# end

File "/usr/lib/python2.4/socket.py", line 169, in accept
sock, addr = self._sock.accept()
File "ex_server.py", line 28, in cleanup
server.shutdown(socket.SHUT_RDWR)
File "<string>", line 1, in shutdown
socket.error: (128, 'Transport endpoint is not connected')
Well... You shutdown I/O on a socket that was waiting for a
connection to be made. Seems like a reasonable message...
Does anybody know a better way to do this?
Put a try/except block around the shutdown call and consider this
particular error as a normal possibility. I've also interspersed a few
lines to let you cleanly shutdown the thread.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top