[PYTHON] I get an error when trying meinheld + WebSocket + mongodb

To the meinheld professor who is indebted to me on a daily basis There is a WebSocketMiddleware that makes it easy to implement WebSockets for existing wsgi apps. If it's a code written by a hetareyaro like me, it's very slow and unusable unless it's taken care of by meinheld, so I can't get up.

So, when I thought about making a recent cool-looking app that uses such WebSocket, Of course, mongodb will be chosen as a DB option with such momentum. yep.

So, mongodb while referring to Sample of applying meinheld WebSocketMiddleware to flask etc. written by the author of meinheld himself. I wonder if it will be like this when I try to build

main.py


from shimehari import Shimehari
from mongoengine import connect
from meinheld import server, middleware
import config


def connectDB():
    connect(config['DB_NAME'])

app = Shimehari(__name__)

if __name__ == '__main__':
    connectDB()
    server.listen(("0.0.0.0", 3000))
    server.run(middleware.WebSocketMiddleware(app))

Try to start

$ (venv) bin/python main.py

People People People People People People

Sudden error <  ̄Y^Y^Y^Y^Y^Y^Y ̄

Trace back

Traceback (most recent call last):
  File "main.py", line 25, in <module>
    connectDB()
  File "/project/path/main.py", line 13, in connectDB
    connect(config['DB_NAME'])
  File "/project/path/lib/python2.7/site-packages/mongoengine/connection.py", line 162, in connect
    return get_connection(alias)
  File "/project/path/lib/python2.7/site-packages/mongoengine/connection.py", line 126, in get_connection
    raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e))
mongoengine.connection.ConnectionError: Cannot connect to database default :
call from same greenlet

I don't know what you're talking about. I wondered if the host around the port was strange Suddenly I changed the implementation as follows and it worked.

main.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-

from shimehari import Shimehari


def connectDB():
    connect(config['DB_NAME'])

app = Shimehari(__name__)

if __name__ == '__main__':
    connectDB()
    #import after connecting to db
    from meinheld import server, middleware
    server.listen(("0.0.0.0", 3000))
    server.run(middleware.WebSocketMiddleware(app))

It's a natural thing for the mohawk people who are messing around with it! It may be something like that I didn't come out at all when I googled I think that there are more people who use node.js when doing WebSocket, so I think that's the case, while thinking For the time being, it worked with this, so I'm glad Is it better to replace connecetDB with something like with?

Recommended Posts

I get an error when trying meinheld + WebSocket + mongodb
When I get an error with PyInstaller
I get an error when trying to install maec 4.0.1.0 with pip
I get an error when I put opencv in pyautoGUI
I get [Error 2055] when trying to connect to MySQL on Heroku
I get an error with import pandas.
When I get an error with Pylint in Atom on Windows
I got an error when trying to install Xgboost and its solution
What to do if you get an error when trying to load mnist
I got an error when saving with OpenCV
When I get a chromedriver error in Selenium
I get an error with all yum commands
Workaround if you get an error when trying to install PySide with pip
I got an error when trying to run Hello World in Go language
I get an Import Error in Python Beautiful Soup
When you get an error in python scraping (requests)
I got an error when using Tensorboard with Pytorch
I get an error when installing scipy on t2.micro because I don't have enough memory
I get an error when I try to raise Python to 3 series using pyenv on Catalina
What to do if you get an Undefined error when trying to use pip with pyenv
I get an ImportError on appengine_config.py
I got an error when I ran composer global require laravel / installer
I get an OS Error: [Errno 8] Exec format error when running a Flask application with a python command
I got an error when I ran meteor add accounts-password and got hooked
I get an error when I put a Python plugin in Visual Studio Code under the pyenv environment
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
When I try to upgrade pip, I get an infinite loop after failing to upgrade
What to do if you get an error when installing Dlib (Ubuntu)
What to do if you get an error when trying to send a message in tasks.loop () immediately after startup
I get an error ~ is zero, singular U when passing the covariance matrix from the Linear layer to MultivariateNormal
[Python] I want to know the variables in the function when an error occurs!
When I name the file flask.py in Flask, I get Import Error: cannot import name'Flask'
I got an error when I put opencv in python3 with Raspberry Pi [Remedy]
An error occurs when trying to import scikit-learn after connecting to Oracle with SQLAlchemy
What to do if you get an error when installing python with pyenv
If you get a long error when tabbing an interactive shell with Anaconda
I tried to get an image by scraping
I get a UnicodeDecodeError when running with mod_wsgi
Rollback processing when an error occurs with fabric
Error when trying to install psycopg2 in Python
An error occurs when importing japandas (problem unsolved)
What to do if you get an OpenSSL error when installing Python 2 with pyenv
When I try to import pandas on macOS I get the error No module named'_bz2'
When I try to install mysqlclient with Django, I get error: command'gcc' failed with exit status 1.
I want to get an error message in Japanese with django Password Change Form
What to do if you get an Import Error when importing matplotlib with Jupyter
I got an error when pip install pandas on Mac, so I dealt with it