in Education by
(I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably work if you run python 2.6 or use the multiprocessing backport) I currently have a program that listens to a unix socket (using a processing.connection.Listener), accept connections and spawns a thread handling the request. At a certain point I want to quit the process gracefully, but since the accept()-call is blocking and I see no way of cancelling it in a nice way. I have one way that works here (OS X) at least, setting a signal handler and signalling the process from another thread like so: import processing from processing.connection import Listener import threading import time import os import signal import socket import errno # This is actually called by the connection handler. def closeme(): time.sleep(1) print 'Closing socket...' listener.close() os.kill(processing.currentProcess().getPid(), signal.SIGPIPE) oldsig = signal.signal(signal.SIGPIPE, lambda s, f: None) listener = Listener('/tmp/asdf', 'AF_UNIX') # This is a thread that handles one already accepted connection, left out for brevity threading.Thread(target=closeme).start() print 'Accepting...' try: listener.accept() except socket.error, e: if e.args[0] != errno.EINTR: raise # Cleanup here... print 'Done...' The only other way I've thought about is reaching deep into the connection (listener._listener._socket) and setting the non-blocking option...but that probably has some side effects and is generally really scary. Does anyone have a more elegant (and perhaps even correct!) way of accomplishing this? It needs to be portable to OS X, Linux and BSD, but Windows portability etc is not necessary. Clarification: Thanks all! As usual, ambiguities in my original question are revealed :) I need to perform cleanup after I have cancelled the listening, and I don't always want to actually exit that process. I need to be able to access this process from other processes not spawned from the same parent, which makes Queues unwieldy The reasons for threads are that: They access a shared state. Actually more or less a common in-memory database, so I suppose it could be done differently. I must be able to have several connections accepted at the same time, but the actual threads are blocking for something most of the time. Each accepted connection spawns a new thread; this in order to not block all clients on I/O ops. Regarding threads vs. processes, I use threads for making my blocking ops non-blocking and processes to enable multiprocessing. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Isnt that what select is for?? Only call accept on the socket if the select indicates it will not block... The select has a timeout, so you can break out occasionally occasionally to check if its time to shut down....

Related questions

0 votes
    (I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 7, 2022 in Education by JackTerrance
0 votes
    (I'm using the pyprocessing module in this example, but replacing processing with multiprocessing should probably ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 6, 2022 in Education by JackTerrance
0 votes
    I have set up a cronjob to run a script daily. This script pulls out a list of Ids from a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    I have set up a cronjob to run a script daily. This script pulls out a list of Ids from a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever ... is it necessary? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Which value of Socket.readyState atribute of WebSocket indicates that the connection is going through the closing handshake? A - 0 B - 1 C - 2 D - 3...
asked Dec 2, 2020 in Technology by JackTerrance
0 votes
    I am trying to understand the advantages of multiprocessing over threading. I know that multiprocessing gets around the ... same thing? Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    Which of them is not a proper step in competitive intelligence data processing? (a) Data gathering (b) Data ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 2, 2021 in Education by JackTerrance
0 votes
    Which one of the following is a proper step in competitive intelligence data processing? (a) Competitors' data ... questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 2, 2021 in Education by JackTerrance
0 votes
    I created the below method to find an Analysis object, update the results field on it and then lastly ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    I know we are all using a bunch of ORMs for SQL, but I wanted to give a try to native drivers. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 26, 2022 in Education by JackTerrance
0 votes
    Which of them is not a proper way of getting into the system? (a) IM (b) Attachments (c) Official ... ?,Need-for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
0 votes
    Which of the following is not a proper way of how spammers get the email Ids? (a) When a user ... ,Need-for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    I got this question regarding web server (such as nginx, Cherokee or Oracle iPlanet) and Java containers ( ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
...