Lawouach / WebSocket-for-Python

WebSocket client and server library for Python 2 and 3 as well as PyPy (ws4py 0.5.1)
https://ws4py.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
1.12k stars 289 forks source link

CherryPy goes down after start #244

Open crocodilered opened 6 years ago

crocodilered commented 6 years ago

Hello.

I've arranged test project to learn ws4py and it worked perfect. But when I moved code to development env CherryPy start to go down right after start:

[28/Jun/2018:14:50:39] ENGINE Bus STARTING
[28/Jun/2018:14:50:39] ENGINE Starting WebSocket processing
[28/Jun/2018:14:50:39] ENGINE SA plugin started
[28/Jun/2018:14:50:39] ENGINE Setting up Mako resources
[28/Jun/2018:14:50:39] ENGINE VideoGenerator plugin started.
[28/Jun/2018:14:50:39] ENGINE Serving on http://192.168.51.74:8080
[28/Jun/2018:14:50:39] ENGINE Bus STARTED
[...]\Python\Python36\lib\site-packages\cherrypy\process\wspbus.py:258: RuntimeWarning: The main thread is exiting, but the Bus is in the states.STARTED state; shutting it down automatically now. You must either call bus.block() after start(), or call bus.exit() before the main thread exits.
  'main thread exits.' % self.state, RuntimeWarning)
[28/Jun/2018:14:50:39] ENGINE Bus STOPPING
[28/Jun/2018:14:50:40] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('192.168.51.74', 8080)) shut down
[28/Jun/2018:14:50:40] ENGINE Terminating WebSocket processing
[28/Jun/2018:14:50:40] ENGINE VideoGenerator plugin stopped.
[28/Jun/2018:14:50:40] ENGINE Freeing up Mako resources
[28/Jun/2018:14:50:40] ENGINE SA plugin stopped.
[28/Jun/2018:14:50:40] ENGINE Bus STOPPED
[28/Jun/2018:14:50:40] ENGINE Bus EXITING
[28/Jun/2018:14:50:40] ENGINE Bus EXITED

Process finished with exit code 0

If I remove WebSocketPlugin(cherrypy.engine).subscribe() it starts okay but i need this plugin :-) Any suggestions how to force it work?

crocodilered commented 6 years ago

Complete code of server.py, that starts CP:

import os
import cherrypy
from ws4py.server.cherrypyserver import WebSocketPlugin, WebSocketTool
from webapp.libs.plugins.saplugin import SaPlugin
from webapp.libs.plugins.makoplugin import MakoTemplatePlugin
from webapp.libs.plugins.video_generator_plugin import VideoGeneratorPlugin
from webapp.libs.tools.makotool import MakoTool
from webapp.libs.tools.authtool import AuthTool
from webapp.libs.tools.satool import SaTool
from webapp.libs.ws_handler import WsHandler

def error_page(status, message, traceback, version):
    """ HTTP errors handler """
    if cherrypy.response.status == 401:
        s = open('webapp/templates/errors/401.html', 'r', encoding='UTF-8').read()
        return_url = '%s?%s' % (cherrypy.request.path_info, cherrypy.request.query_string)
        return s % return_url.replace('?', '%3F').replace('/', '%2F')
    elif cherrypy.response.status == 404:
        return open('webapp/templates/errors/404.html', 'rb')

#########################################################
# Tools
#
cherrypy.tools.sa = SaTool()
cherrypy.tools.render = MakoTool()
cherrypy.tools.auth = AuthTool()
cherrypy.tools.websocket = WebSocketTool()

#########################################################
# Controllers
#
from webapp.controllers.app import App
from webapp.controllers.calculation import Calculation
from webapp.controllers.api import Api
from webapp.controllers.ws import WsController

app = App()
app.calculation = Calculation()
app.api = Api()
app.ws = WsController()

#########################################################
# Config
#
curr_dir = os.path.abspath(os.path.dirname(__file__))
conf_file = os.path.join(curr_dir, 'conf', 'server.conf')
application = cherrypy.tree.mount(app, '/', conf_file)
cherrypy.config.update(conf_file)
cherrypy.config.update({
    '/ws': {
        'tools.websocket.on': True,
        'tools.websocket.handler_cls': WsHandler
    }
})
cherrypy.config.update({'error_page.401': error_page})
cherrypy.config.update({'error_page.404': error_page})

#########################################################
# Plugins
#
MakoTemplatePlugin(cherrypy.engine,
                   os.path.join(curr_dir, 'templates'),
                   os.path.join(curr_dir, 'templates', '.cache')).subscribe()

ce_config = application.config['CalculationsEngine']
VideoGeneratorPlugin(cherrypy.engine,
                     ce_config['ce.ticks_to_generate_movie'],
                     ce_config['ce.movie_frame_rate']).subscribe()

db_config = application.config['Database']
mysql_connection_string = 'mysql://%s:%s@%s:%s/%s?charset=utf8' % (db_config['mysql.user'],
                                                                   db_config['mysql.password'],
                                                                   db_config['mysql.host'],
                                                                   db_config['mysql.port'],
                                                                   db_config['mysql.database'])
SaPlugin(cherrypy.engine, mysql_connection_string).subscribe()
WebSocketPlugin(cherrypy.engine).subscribe()

if __name__ == '__main__':
    # For dev purpose
    cherrypy.engine.start()