473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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

Oct 27 '06 #1
2 4197
mumebuhi wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Oct 27 '06 #2
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

Oct 27 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mike M | last post by:
Is it possible? In the parent process, I have a socket that binds, listens and then accepts new connections (which creates new sockets in the process). I want to be able to pass some of these new...
3
by: Saizan | last post by:
I embedded an Rpyc threaded server in a preexistent daemon (an irc bot), this is actually very simple; start_threaded_server(port = DEFAULT_PORT) then I had the necessity to stop the thread which...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
0
by: mumebuhi | last post by:
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...
5
by: darthghandi | last post by:
I've created a class to listen to all interfaces and do a BeginAccept(). Once it gets a connection, it passes the connected socket off and stores it in a List. Next, it continues to listen for...
0
by: shonen | last post by:
Hi guys! Alright so here is my question, I've been messing around with irclib in my python programs creating a bot. I just want to add a little bit of functionality to it! So basically, what...
10
by: ThunderMusic | last post by:
Hi, I'm currently working with sockets. I accept connections using m_mySocket.Listen(BackLogCount); But when I want to stop listening, I shutdown all my clients and call m_mySocket.Close(), but it...
2
by: Alexandru Mosoi | last post by:
supposing that I have a server (an instance of SocketServer()) that waits for a connection (ie is blocked in accept()) and in another thread i want to stop the server, how do I do that?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.