473,587 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(th reading.Thread) :
def run(self):
global counter
while True:
counter += 1
time.sleep(10)

worker_thread = WorkerThread()
worker_thread.s tart()

server = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
server.bind(('' , 2727))
server.listen(2 )

def cleanup(signal, frame):
print "die now"
worker_thread.j oin(1)
server.shutdown (socket.SHUT_RD WR)
sys.exit(0)
signal.signal(s ignal.SIGINT, cleanup)

while True:
channel, details = server.accept()
stats = { 'key': "value" }
s = simplejson.dump s(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.acce pt()
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 4242
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(th reading.Thread) :
def run(self):
global counter
while True:
counter += 1
time.sleep(10)

worker_thread = WorkerThread()
worker_thread.s tart()

server = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
server.bind(('' , 2727))
server.listen(2 )

def cleanup(signal, frame):
print "die now"
worker_thread.j oin(1)
server.shutdown (socket.SHUT_RD WR)
sys.exit(0)
signal.signal(s ignal.SIGINT, cleanup)

while True:
channel, details = server.accept()
stats = { 'key': "value" }
s = simplejson.dump s(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.acce pt()
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.acce pt()
File "../../../scratch/python/tmp.py", line 33, in cleanup
server.shutdown (socket.SHUT_RD WR)
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
6828
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 sockets to a spawned process. Is it possible, and if so how? Any help is much appreciated!! Mike
3
5739
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 accept() new connections without killing the whole app, the thread is simply a while True that spawn a new thread which serves each connection, so I...
3
4280
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 receiver is running within Process1. If I move the receiver into Process2 then its fast. Please can someone explain.
0
3153
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 System.Collections.Generic; using System.Net; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary;
0
1826
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 script completely. The script basically does the following. The main thread creates a new thread, which does a completely useless thing, and then starts...
5
12765
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 more incoming connections and does the BeginAccpet() again. It does an infinite loop this way. My question is: What is the best way to stop this? ...
0
1041
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 i have in mind is that it reads a socket, pulls the information that I want from the socket (parses it all up and gives me what i want), and updates...
10
5155
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 always raise a OnConnect event (actually, it calls the callback function as if there was a new connection attempt) and I receive a...
2
2037
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
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7852
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.