Comments (11)
🎉😊
That was great!! Just what I was looking for!! I have tried it and it runs ok. So you can close this issue.
But there is a bug in the code of stackoverflow. If you pass both keyword arguments, then the next exception is raised:
TypeError: notifications_job() got an unexpected keyword argument 'args'
So you have to pass only
thread = socketio.start_background_task(notifications_job, (current_app._get_current_object()))
Thank you very much!!!
from flask-socketio-chat.
Did you monkey patch the standard library?
from flask-socketio-chat.
Miguel, sorry for my late reply.
No, I didn't. I will try it. Nevertheless, I'm also using celery. Would be better using a scheduled celery task or a socketio background task for this purpose?
from flask-socketio-chat.
That really depends. But regardless of your choice, you need to monkey patch, so start with that.
from flask-socketio-chat.
Hi Miguel, I have monkey patched the app with no success.
Here you can find my own repository with a code example https://github.com/j2logo/Flask-SocketIO-AppContext
As you can see, I have followed the same approach as in your chat example with the exception of accessing to the app context.
This code doesn't work (no event is sent):
from threading import Lock
from flask import Flask
from app import create_app, socketio
thread = None
thread_lock = Lock()
def notifications_job():
app = create_app()
count = 0
with app.app_context():
while True:
step = int(app.config.get('STEP', 1))
count += step
print("Count: {}".format(count))
socketio.emit('my_response', {'count': count}, namespace='/rt/notifications/')
socketio.sleep(10)
@socketio.on('connect', namespace='/rt/notifications/')
def start_notifications_thread():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=notifications_job)
However, this another one does:
from threading import Lock
from flask import Flask
from app import create_app, socketio
thread = None
thread_lock = Lock()
def custom_create_app():
app = Flask(__name__)
app.config.from_object('config')
# NO socketio initialization
# Blueprints registration (only real time events blueprint)
from app.rt import real_time_events
app.register_blueprint(real_time_events)
return app
def notifications_job():
app = custom_create_app()
count = 0
with app.app_context():
while True:
step = int(app.config.get('STEP', 1))
count += step
print("Count: {}".format(count))
socketio.emit('my_response', {'count': count}, namespace='/rt/notifications/')
socketio.sleep(10)
@socketio.on('connect', namespace='/rt/notifications/')
def start_notifications_thread():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=notifications_job)
I think that socketio.init_app(app) in init.py is the key.
What do you think? Is custom_create_app
a good approach? Do you have any example accessing to the app context?
from flask-socketio-chat.
Three comments:
- I don't see any monkey patching in your application.
- You are creating a different app instance for your background task. You need to use the same app instance.
- You are creating a
real_time_events
blueprint that is completely unnecessary, since Socket.IO events are not attached to a blueprint.
from flask-socketio-chat.
Miguel sorry for my trouble.
I have committed a new version of my app https://github.com/j2logo/Flask-SocketIO-AppContext. The only way I have found to run the socketio thread is defining it in the run.py
module but I don't like this approach. I would like having it in the events.py
module.
Any suggestion?
from flask-socketio-chat.
What is the problem if you move the Socket.IO code down to a events.py
? I did that in the application in this repo and had no issues. In fact, this repo exists to demonstrate how to do it because a lot of people needed an actual example.
from flask-socketio-chat.
The problem is accessing to the app context.
If I follow your example, I have no trouble:
This would be the events.py
:
from threading import Lock
from .. import socketio
thread = None
thread_lock = Lock()
def notifications_job():
count = 0
#with app.app_context():
# step = int(app.config.get('STEP', 1))
while True:
socketio.sleep(10)
count += 1
print("Count: {}".format(count))
socketio.emit('my_response', {'count': count}, namespace='/rt/notifications/')
@socketio.on('connect', namespace='/rt/notifications/')
def start_notifications_thread():
global thread
with thread_lock:
if thread is None:
thread = socketio.start_background_task(target=notifications_job)
But if I change the notifications_job
function as below I think that some socketio
or app
import is wrong:
from threading import Lock
from .. import socketio
from run import app
thread = None
thread_lock = Lock()
def notifications_job():
count = 0
with app.app_context():
step = int(app.config.get('STEP', 1))
while True:
socketio.sleep(10)
count += 1
print("Count: {}".format(count))
socketio.emit('my_response', {'count': count}, namespace='/rt/notifications/')
Have you ever access to app context within socketio?
from flask-socketio-chat.
You can pass the app
instance as an argument into your thread. I actually answered a question on Stack Overflow about this same thing yesterday: https://stackoverflow.com/questions/49252601/flask-socketio-context-for-flask-sqlalchemy/49271277?noredirect=1#comment85550170_49271277
from flask-socketio-chat.
This issue will be automatically closed due to being inactive for more than six months. Seeing that I haven't responded to your last comment, it is quite possible that I have dropped the ball on this issue and I apologize about that. If that is the case, do not take the closing of the issue personally as it is an automated process doing it, just reopen it and I'll get back to you.
from flask-socketio-chat.
Related Issues (20)
- Trying to make a chatbot HOT 3
- 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
- Making calls to external API with only 1 gunicorn worker HOT 12
- 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.