Python SocketServer

2024/9/24 19:21:48

How can I call shutdown() in a SocketServer after receiving a certain message "exit"? As I know, the call to serve_forever() will block the server.

Thanks!

Answer

Use the source, Luke!

Excerpt from SocketServer.py:

   def serve_forever(self, poll_interval=0.5):"""Handle one request at a time until shutdown.Polls for shutdown every poll_interval seconds. Ignoresself.timeout. If you need to do periodic tasks, do them inanother thread."""self.__is_shut_down.clear()try:while not self.__shutdown_request:# XXX: Consider using another file descriptor or# connecting to the socket to wake this up instead of# polling. Polling reduces our responsiveness to a# shutdown request and wastes cpu at all other times.r, w, e = select.select([self], [], [], poll_interval)if self in r:self._handle_request_noblock()finally:self.__shutdown_request = Falseself.__is_shut_down.set()def shutdown(self):"""Stops the serve_forever loop.Blocks until the loop has finished. This must be called whileserve_forever() is running in another thread, or it willdeadlock."""self.__shutdown_request = Trueself.__is_shut_down.wait()
https://en.xdnf.cn/q/71668.html

Related Q&A

Loading JSON file in BigQuery using Google BigQuery Client API

Is there a way to load a JSON file from local file system to BigQuery using Google BigQuery Client API?All the options I found are:1- Streaming the records one by one.2- Loading JSON data from GCS.3- …

How to extract tables from a pdf with PDFMiner?

I am trying to extract information from some tables in a pdf document. Consider the input:Title 1 some text some text some text some text some text some text some text some text some text some textTabl…

Draw Box-Plot with matplotlib

Is it possible to plot this kind of chart with matplotlib?

Why I get urllib2.HTTPError with urllib2 and no errors with urllib?

I have the following simple code:import urllib2 import sys sys.path.append(../BeautifulSoup/BeautifulSoup-3.1.0.1) from BeautifulSoup import * page=http://en.wikipedia.org/wiki/Main_Page c=urllib2.urlo…

python - replace the boolean value of a list with the values from two different lists [duplicate]

This question already has answers here:Merge two or more lists with given order of merging(2 answers)Closed 6 years ago.I have one list with boolean values likelyst = [True,True,False,True,False]and tw…

Convert pandas DataFrame to dict and preserve duplicated indexes

vagrant@ubuntu-xenial:~/lb/f5/v12$ python Python 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609] on linux2 Type "help", "copyright", "credits" or "licens…

Drawing rectangle on top of data using patches

I am trying to draw a rectangle on top of a data plot in matplotlib. To do this, I have this codeimport matplotlib.patches as patches import matplotlib.pyplot as pl...fig = pl.figure() ax=fig.add_axes(…

Setting row edge color of matplotlib table

Ive a pandas DataFrame plotted as a table using matplotlib (from this answer).Now I want to set the bottom edge color of a given row and Ive this code:import pandas as pd import numpy as np import matp…

TypeError: string indices must be integers (Python) [duplicate]

This question already has answers here:Why am I seeing "TypeError: string indices must be integers"?(10 answers)Closed 5 years ago.I am trying to retrieve the id value : ad284hdnn.I am getti…

how to split numpy array and perform certain actions on split arrays [Python]

Only part of this question has been asked before ([1][2]) , which explained how to split numpy arrays. I am quite new in Python. I have an array containing 262144 items and want to split it in small…