ich will ein HTTP server haben der jede anfrage in einen separaten thread behandelt.
hier der 2. versuch:
Code: Alles auswählen
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from time import sleep
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
sleep(10)
self.wfile.write("Hello World!")
class ThreadingHTTPServer(ThreadingMixIn, HTTPServer):
pass
server = ThreadingHTTPServer(("localhost", 8080), Handler)
server.serve_forever()
Code: Alles auswählen
>>> import multiprocessing
>>> p = multiprocessing.Pool(5)
>>> def f(i): urllib2.urlopen("http://localhost:8080").read()
>>> import urllib2
>>> p.map_async(f, range(5))
<multiprocessing.pool.MapResult object at 0x0000000002BF64A8>
>>> _.get(timeout=15)
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
_.get(timeout=15)
File "C:\Python27\lib\multiprocessing\pool.py", line 518, in get
raise TimeoutError
TimeoutError
>>>
thx!