Comments (12)
The eventlet worker can handle multiple requests at once.
Your problem could be that you haven't monkey patched the Python standard library so that it is compatible with eventlet. Gunicorn normally does that, but I believe that is currently broken, so you should monkey patch in your application.
from flask-socketio-chat.
Hi Miguel,
Thank you so much for the help and I am sorry for the multiple posts. Thank you also for being so helpful in this community. It is amazing.
I am still having trouble even when I monkey patch.
Below is the top of my app.py:
`# import eventlet
eventlet.monkey_patch()
import gevent.monkey
gevent.monkey.patch_all()
async_mode = 'gevent'
import os
import re
import csv
import requests
from flask import Flask, request, jsonify, json, current_app, render_template, send_file, make_response, redirect
import json
import math
import secrets
import datetime
import jwt
from threading import Thread
from firebase_admin import credentials, firestore, initialize_app
import time
import hashlib
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from flask_socketio import SocketIO, join_room, leave_room, send
import uuid
from gevent import monkey
Initialize Firestore DB
cred = credentials.Certificate('key.json')
default_app = initialize_app(cred)
db = firestore.client()
Users = db.collection('Users')
WebsiteAnalytics = db.collection('Website Analytics')
Chats = db.collection('Chats')
app = Flask(name)
app.config['SECRET_KEY'] = 'gjr39dkjn344_!67#'
socketio = SocketIO(app, engineio_logger=True, logger=True, message_queue='redis://', async_mode=async_mode)`
The logs on my server whenever I try an endpoint with a Firebase call is so:
(env) 253-3:crush jamesbaker$ gunicorn --error-logfile - --worker-class gevent -w 1 app:app [2020-03-29 12:04:28 -0400] [30435] [INFO] Starting gunicorn 20.0.4 [2020-03-29 12:04:28 -0400] [30435] [INFO] Listening at: http://127.0.0.1:8000 (30435) [2020-03-29 12:04:28 -0400] [30435] [INFO] Using worker: gevent [2020-03-29 12:04:28 -0400] [30438] [INFO] Booting worker with pid: 30438 Server initialized for gevent. [2020-03-29 12:05:14 -0400] [30435] [CRITICAL] WORKER TIMEOUT (pid:30438) [2020-03-29 12:05:14 -0400] [30438] [INFO] Worker exiting (pid: 30438) [2020-03-29 12:05:14 -0400] [30445] [INFO] Booting worker with pid: 30445 Server initialized for gevent.
Same goes when I use eventlet:
(env) 253-3:crush jamesbaker$ gunicorn --error-logfile - --worker-class eventlet -w 1 app:app [2020-03-29 12:01:09 -0400] [30421] [INFO] Starting gunicorn 20.0.4 [2020-03-29 12:01:09 -0400] [30421] [INFO] Listening at: http://127.0.0.1:8000 (30421) [2020-03-29 12:01:09 -0400] [30421] [INFO] Using worker: eventlet [2020-03-29 12:01:09 -0400] [30424] [INFO] Booting worker with pid: 30424 Server initialized for eventlet. [2020-03-29 12:01:53 -0400] [30421] [CRITICAL] WORKER TIMEOUT (pid:30424) [2020-03-29 12:01:53 -0400] [30424] [INFO] Worker exiting (pid: 30424) [2020-03-29 12:01:53 -0400] [30430] [INFO] Booting worker with pid: 30430 Server initialized for eventlet.
Any idea what it could be? I have been trying to debug for a while now.
from flask-socketio-chat.
So when I printed eventlet.patcher.is_monkey_patched(firestore)
it returned false. So maybe eventlet isn't monkey_patching my firebase module?
from flask-socketio-chat.
I don't think there is a lot to say about it. The monkey patching replaces the functions that are incompatible with eventlet with versions that are compatible. It's better to use packages that support greenlets natively, but for the vast majority of applications monkey patching is the only way, since there aren't a lot of packages written specifically for greenlets.
Found your comment from a separate thread^
Is it fair to assume that Firebase is not compatible with gunicorn and eventlet/gevent?
from flask-socketio-chat.
The monkey patching is for the standard library, there is no specific monkey patching for third-party packages, other than a few exceptions. Looking at firebase, it is sadly depends on Google SDKs for Python, which sadly do not work well with eventlet/gevent. See grpc/grpc#15923.
from flask-socketio-chat.
Makes sense! Thank you for all the help.
Noting somewhere that eventlet/gevent only is compatible with a certain set of Python packages may be useful for other users.
from flask-socketio-chat.
This is not something specific to eventlet and gevent, it is a characteristic of all async frameworks. It is true also for asyncio.
If you have any suggestions about how to include this let me know. I don't see how to explain how to use an async framework in a short note.
from flask-socketio-chat.
Nowhere under running with gunicorn does it mention monkey patching or the fact that it is not a panacea for all async issues when running with gunicorn. It just would be worth mentioning that considering it has taken a non-zero amount of time from myself along with others who I saw run into this problem elsewhere - especially considering when the project works without gunicorn just fine. Thank you for all that you do for the flask and socketio communities.
https://github.com/miguelgrinberg/Flask-SocketIO/blob/master/docs/index.rst#gunicorn-web-server
from flask-socketio-chat.
Gunicorn is currently broken on this respect, it should monkey patch for you. Your issue is unfortunate, but under normal circumstances you would have not seen problems related to no monkey patching.
Also as I was saying, async frameworks are very complex, you can't just say "monkey patch" and the problems go away. For many situations monkey patching makes things worse. Sometimes you need to monkey patch some functions, but leave others alone. Sometimes monkey patching does not solve your problem. I could add a note to cover your own case, but there are about a million other cases that are completely different than yours for which the advice given to you would be wrong and misleading.
from flask-socketio-chat.
You don't think it would be useful to note that Gunicorn is not currently correctly monkey patching and that it doesn't work perfectly for some "million other cases"?
from flask-socketio-chat.
No. Monkey patching isn't specific to gunicorn, so relying on gunicorn's automatic patching isn't a good idea anyway. If your application needs monkey patching you will want to monkey patch on your own, at the very least so that you can also run your app in development, where more than likely you will not be using gunicorn. So I do not believe documenting bugs in gunicorn wrt monkey patching in my documentation is necessary.
There are already several references to monkey patching in the documentation. Have you seen them? If you have suggestions for more, feel free to suggest where in a PR.
from flask-socketio-chat.
Okay, you win Miguel. I just hope someone who has this same issue sees this post in a timely manner and doesn't waste their time as I did.
from flask-socketio-chat.
Related Issues (20)
- Trying to make a chatbot HOT 3
- Accessing to application context in a background task with Blueprints HOT 11
- Scaling with Redis HOT 1
- Server receives event correctly, but It doesn't execute method HOT 1
- Unicode message seems not work with SSL HOT 4
- Not working on Google chrome HOT 2
- Understanding the structure HOT 3
- Commands HOT 3
- Clean install virtualenv error HOT 2
- Flask-socketio nginx HOT 11
- socketio.run (app) vs app.run ()
- way to change the hosted socket HOT 2
- Add Password Support: HOT 1
- Flask-SocketIO-Chat with background event
- didn't work on localhost cuz of `https` missing from source HOT 4
- Socket Handlers not being Triggered Even after i've imported them HOT 13
- Example code with Auth key at the time of connect HOT 1
- python version HOT 2
- failed to install requirements on windows due to greenlet HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flask-socketio-chat.