How to serve no more that one HTTP request in python?
<p>I have a simple HTTP server setup like <a href="http://stackoverflow.com/a/11769969">this one</a>. It processes a slow 40 second request to open and then close gates (real metallic gates). If second HTTP query is made during execution of the first one, it is placed in queue and then executed after first run. I don't need this behavior, I need to reply with error if gate open/close procedure is in progress now.
How can I do that? There's a parameter 'request_queue_size' - but I'm not sure how to set it.</p>

<p>'request_queue_size' seems to have no effect.
The solution was to make server multithreaded, and implement locking variable 'busy':</p>

<pre><code>from socketserver import ThreadingMixIn
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
from gpiozero import DigitalOutputDevice
import logging
from time import sleep
logging.basicConfig(format='%(asctime)s %(levelname)s:%(message)s', level=logging.INFO)

hostName = ''
hostPort = 9001
busy = False

class ThreadingServer(ThreadingMixIn, HTTPServer):
    pass

class MyServer(BaseHTTPRequestHandler):
    def do_GET(self):
        global busy
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()
        self.wfile.write(bytes("Hello!&lt;br&gt;", "utf-8"))
        if self.path == '/gates':
           if not busy:
             busy = True
             relay = DigitalOutputDevice(17) # Initialize GPIO 17
             relay.on()
             logging.info('Cycle started')
             self.wfile.write(bytes("Cycle started&lt;br&gt;", "utf-8"))
             sleep(2)
             relay.close()
             sleep(20)
             relay = DigitalOutputDevice(17)
             relay.on()
             sleep(2)
             relay.close()
             logging.info('Cycle finished')
             self.wfile.write(bytes("Cycle finished", "utf-8"))
             busy = False
           else:
#             self.wfile.write(bytes("Busy now!&lt;br&gt;", "utf-8"))
             self.send_error(503)

myServer = ThreadingServer((hostName, hostPort), MyServer)
print(time.asctime(), "Server Starts - %s:%s" % (hostName, hostPort))

try:
    myServer.serve_forever()
except KeyboardInterrupt:
    pass

myServer.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (hostName, hostPort))
</code></pre>

python