469,160 Members | 1,832 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,160 developers. It's quick & easy.

To Kill a Process that is Accepting Connection on Socket

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

Oct 27 '06 #1
0 1537

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Mike M | last post: by
10 posts views Thread by Jako Menkveld | last post: by
2 posts views Thread by Mitch Mooney | last post: by
3 posts views Thread by Thomas Dybdahl Ahle | last post: by
4 posts views Thread by Richard Rossel | last post: by
18 posts views Thread by =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.