2015-10-23 34 views
2

我有一個簡單的Web應用程序,它使用CherryPy作爲RESTful後端,ws4py作爲websockets。Websocket(Python 3.5中的ws4py)不能在Docker容器中工作

1) Javascript client sends a POST to Python backend 
2) Python replies with a unique id 
3) Javascript connects to the websocket and sends the unique id 
4) Python associates the websocket client connection to the unique id 

的Python代碼的WebSocket:

class WebSocketHandler(WebSocket): 
def __init__(self, *args, **kw): 
    WebSocket.__init__(self, *args, **kw) 
    print('Connected to websocket server!') 
    SUBSCRIBERS.add(self) 

def received_message(self, message): 
    print('Websocket identified itself?') 
    SUBSCRIBERS_WITH_ID[message.data] = self 

的Python的CherryPy代碼:

cherrypy.config.update({'server.socket_host': 0.0.0.0, 
         'server.socket_port': 8080}) 

cherrypy.tools.CORS = cherrypy.Tool('before_handler', CORS) 

WebSocketPlugin(cherrypy.engine).subscribe() 
cherrypy.tools.websocket = WebSocketTool() 

cherrypy.tree.mount(
    DummyAPI(), '/api/dummy', config={ 
     '/': { 
      'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 
      'tools.CORS.on': True 
     } 
    } 
) 

cherrypy.tree.mount(
    WebSocketAPI(), '/ws', config={ 
     '/': { 
      'tools.websocket.on': True, 
      'tools.websocket.handler_cls': WebSocketHandler 
     } 
    } 
) 

cherrypy.engine.start() 
cherrypy.engine.block() 

Javascript代碼:

POST到API:

var req = new XMLHttpRequest(); 
req.onload = function (e) { 
    console.log('Response code: ' + req.responseText); 
    result_id = req.responseText; 
    websocketConnect(); 
}; 
req.open('POST', api_url); 
req.send(); 

的WebSocket部分:

function websocketConnect() { 
websocket = new WebSocket(websocket_url); 
websocket.onopen = function (evt) { 
     console.log("Connected to WebSocket server."); 
     websocket.send(result_id)}; 

能正常工作在我的機器上,並部署在Amazon EC2上。但是,當我嘗試將它部署到Docker容器中時,這些websocket不起作用。

這裏是我的Dockerfile:

FROM python 
MAINTAINER xxx 

RUN pip3.5 install cherrypy ws4py 
RUN mkdir /var/www 

COPY python /var/www/python 

EXPOSE 8080 

CMD ["/usr/local/bin/python3.5", "/var/www/python/simplews.py"] 

輸出,當我從碼頭工人運行:

sudo docker run -v /tmp/dockertmp/:/tmp/ -p 8080:8080 -i 563a51d13f59 
[23/Oct/2015:10:09:00] ENGINE Bus STARTING 
[23/Oct/2015:10:09:00] ENGINE Starting WebSocket processing 
[23/Oct/2015:10:09:00] ENGINE Started monitor thread '_TimeoutMonitor'. 
[23/Oct/2015:10:09:00] ENGINE Started monitor thread 'Autoreloader'. 
[23/Oct/2015:10:09:01] ENGINE Serving on http://0.0.0.0:8080 
[23/Oct/2015:10:09:01] ENGINE Bus STARTED 
172.17.42.1 - - [23/Oct/2015:10:09:08] "POST ..." 
Connected to websocket server! 

它永遠不會到達那裏的JavaScript文件能夠通過WebSocket的發送數據的階段。 Chrome調試顯示WS狀態爲「待定」。

然而,當我退出在泊塢的CherryPy的應用,網頁套接字消息來通過:

enter code here 
sudo docker run -v /tmp/dockertmp/:/tmp/ -p 8080:8080 -i 563a51d13f59 
[23/Oct/2015:10:15:30] ENGINE Bus STARTING 
[23/Oct/2015:10:15:30] ENGINE Starting WebSocket processing 
[23/Oct/2015:10:15:30] ENGINE Started monitor thread '_TimeoutMonitor'. 
[23/Oct/2015:10:15:30] ENGINE Started monitor thread 'Autoreloader'. 
[23/Oct/2015:10:15:30] ENGINE Serving on http://0.0.0.0:8080 
[23/Oct/2015:10:15:30] ENGINE Bus STARTED 
172.17.42.1 - - [23/Oct/2015:10:15:33] "POST ..." 
Connected to websocket server! 
^C[23/Oct/2015:10:15:35] ENGINE Keyboard Interrupt: shutting down bus 
[23/Oct/2015:10:15:35] ENGINE Bus STOPPING 
[23/Oct/2015:10:15:40] ENGINE HTTP Server  cherrypy._cpwsgi_server.CPWSGIServer(('0.0.0.0', 8080)) shut down 
[23/Oct/2015:10:15:40] ENGINE Stopped thread '_TimeoutMonitor'. 
[23/Oct/2015:10:15:40] ENGINE Terminating WebSocket processing 
[23/Oct/2015:10:15:40] ENGINE Stopped thread 'Autoreloader'. 
[23/Oct/2015:10:15:40] ENGINE Bus STOPPED 
[23/Oct/2015:10:15:40] ENGINE Bus EXITING 
[23/Oct/2015:10:15:40] ENGINE Bus EXITED 
[23/Oct/2015:10:15:40] ENGINE Waiting for child threads to terminate... 
Websocket identified itself? 

輸出當我運行它,而不多克爾:

python3.5 python/simplews.py 
[23/Oct/2015:06:09:45] ENGINE Bus STARTING 
[23/Oct/2015:06:09:45] ENGINE Starting WebSocket processing 
[23/Oct/2015:06:09:45] ENGINE Started monitor thread '_TimeoutMonitor'. 
[23/Oct/2015:06:09:45] ENGINE Started monitor thread 'Autoreloader'. 
[23/Oct/2015:06:09:45] ENGINE Serving on http://0.0.0.0:8080 
[23/Oct/2015:06:09:45] ENGINE Bus STARTED 
127.0.0.1 - - [23/Oct/2015:06:09:48] "POST ..." 
Connected to websocket server! 
Websocket identified itself? 

在非泊塢案件websocket立即標識自己。我無法弄清楚這一點。 WebSocket接收器線程在Docker中以某種方式卡住了?

的可疑部分來自多克爾日誌是應用程序退出時,網頁套接字接收的消息最終到達:

[23/Oct/2015:10:15:40] ENGINE Waiting for child threads to terminate... 
Websocket identified itself? 
+0

您是否嘗試過運行'strace的-p [YOUR_SERVER_PID]',看看你可以看到它卡住了嗎? –

+0

剛剛運行它 - 結果是,多處理模塊以某種方式使整個應用程序棒。當我的應用程序服務POST請求時,它會啓動一個包含pool.starmap_async的進程來生成唯一的ID。也許Docker +多處理不好? – Sevag

回答

相關問題