我的代码很简单。使用BaseHTTPServer和ThreadInMix我想为每个请求同时运行一个python脚本(Script1.py) 。BaseHTTPServer线程不起作用
我的代码 -
from subprocess import PIPE, Popen
from SocketServer import ThreadingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import time
def simple_script(self):
print 'simple_script'
s = Popen('C:/Python27/python C:/Script1.py 5', shell=True,
stdout=PIPE, stderr=PIPE)
out, err = s.communicate()
print out, err
self.wfile.write(out)
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
self.wfile.write('{0}\n'.format(time.asctime()))
simple_script(self)
return
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
pass
if __name__ == '__main__':
server = ThreadedHTTPServer(('', 8080), Handler)
print 'Starting server, use <Ctrl-C> to stop'
server.serve_forever()
"""
# C:/Script1.py
import time, sys
s = time.time()
while True:
if time.time() - s > int(sys.argv[1]):
break
else:
time.sleep(1)
print time.asctime()
"""
我只是发现了但─ 随着网址:http://localhost:8080 如果我打开IE浏览器多标签/浏览器,这只是正常 但是,
如果我打开多个Chrome或Firefox中的标签/页面,页面是否等待上一页?这并不意味着在Chrome或Firefox中进行线程化?任何帮助?由于
嗨响, 你能告诉我你用哪个浏览器触发你的吗?任务? – abarik
嗨响, 我更新了基于我的需要的代码!你可以再试一次,让我知道它是否有效? 谢谢! – abarik
是的,它确实有效。根本不使用浏览器,只是卷曲。 – Ringding