To Stop a Process that is Waiting on socket.accept()

M

mumebuhi

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:
---start---
Traceback (most recent call last):
File "ex_server.py", line 35, 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 30, in cleanup
server.shutdown(SHUT_RDWR)
NameError: global name 'SHUT_RDWR' is not defined
---end---

Can anybody suggest a better way to do this?

Thank you.


Buhi
 
S

Steve Holden

mumebuhi said:
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:
---start---
Traceback (most recent call last):
File "ex_server.py", line 35, 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 30, in cleanup
server.shutdown(SHUT_RDWR)
NameError: global name 'SHUT_RDWR' is not defined
---end---

Can anybody suggest a better way to do this?
You haven't copied the code you are actually running. The program above
may or may not work, but compare the server.shutdown line in the
traceback with the server.shutdown line in the program and you'll see
they aren't the same.

regards
Steve
 
M

mumebuhi

Hi Steve,

The output is as the following (excluding the "---start---" and
"---end---"):
---start---
die now
Traceback (most recent call last):
File "../../../scratch/python/tmp.py", line 39, in ?
channel, details = server.accept()
File "/usr/lib/python2.4/socket.py", line 169, in accept
sock, addr = self._sock.accept()
File "../../../scratch/python/tmp.py", line 33, in cleanup
server.shutdown(socket.SHUT_RDWR)
File "<string>", line 1, in shutdown
socket.error: (128, 'Transport endpoint is not connected')
---end---

I copied and pasted the previous posting and the above is the output.
It is the actual code. Sorry if I don't get your point.


Buhi
 
D

Dennis Lee Bieber

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

The error message from the cut&paste post
File "../../../scratch/python/tmp.py", line 33, in cleanup
server.shutdown(socket.SHUT_RDWR)
File "<string>", line 1, in shutdown
socket.error: (128, 'Transport endpoint is not connected')
---end---

I copied and pasted the previous posting and the above is the output.
It is the actual code. Sorry if I don't get your point.

VS the error message from your original post:
File "ex_server.py", line 30, in cleanup
server.shutdown(SHUT_RDWR)
NameError: global name 'SHUT_RDWR' is not defined
---end---

The code posted in the original message is NOT the same code that
produced the original posted error message.
--
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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top