I need to create a Python application that handles both API (fastAPI) and sockets (socketio). I can't find a way to start both the vunicorn applications in the same python script. Note that I can replace vunicorn with any other library that would allow me to fix this.
Code:
import json
from fastapi import FastAPI
import socketio
import uvicorn
# create API app
app_api = FastAPI()
# create a Socket.IO server and wrap with a WSGI application
sio = socketio.Server(port=8001)
app_sio = socketio.WSGIApp(sio)
#app_api.get("/")
def read_root():
return {"Hello": "World"}
#sio.on('*', namespace="/aws-iam")
def catch_all(event, sid, data):
print(event, sid, data)
I am not sure how I can start both app_api and app_sio. I can't start both from the main thread because uvicorn.run(...) is blocking and only the first call would run.
I tried to start them in two different threads but I got errors:
if __name__ == "__main__":
def start_api():
uvicorn.run("testing.mock_api:app_api", host='127.0.0.1', port=8000, reload=True, debug=True) #, workers=3)
def start_sio():
uvicorn.run("testing.mock_api:app_sio", host='127.0.0.1', port=8001, reload=True, debug=True) # , workers=3)
from threading import Thread
import time
threads = [
Thread(target=start_sio),
Thread(target=start_api),
]
for thread in threads:
thread.start()
time.sleep(999999999999)
With multiple thread I get the errors:
File "/src/testing/mock_api.py", line 55, in start_api
uvicorn.run("testing.mock_api:app_api", host='127.0.0.1', port=8000, reload=True, debug=True) #, workers=3)
File "/usr/local/lib/python3.6/dist-packages/uvicorn/main.py", line 447, in run
ChangeReload(config, target=server.run, sockets=[sock]).run()
File "/usr/local/lib/python3.6/dist-packages/uvicorn/supervisors/basereload.py", line 43, in run
self.startup()
File "/usr/local/lib/python3.6/dist-packages/uvicorn/supervisors/basereload.py", line 62, in startup
signal.signal(sig, self.signal_handler)
File "/usr/lib/python3.6/signal.py", line 47, in signal
handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread
Instead of running the two apps separatly, what about only running the FastAPI application & mount the socketio app on this one ?
To illustrate with an example from this github issue, you could do the following :
import socketio
from fastapi import FastAPI
import uvicorn
app = FastAPI()
sio = socketio.AsyncServer(async_mode='asgi')
socket_app = socketio.ASGIApp(sio)
#sio.on('*', namespace="/aws-iam")
async def catch_all(event, sid, data):
print(event, sid, data)
#app.get("/hello")
async def root():
return {"message": "Hello World"}
app.mount('/', socket_app)
if __name__ == "__main__":
uvicorn.run("main:app", reload=True, port=8080)
It includes switching socketio app to async too
Related
I would like to call my celery task when a websocket client connects to the socket server. When I try calling it from the connect event it causes the application to time out and the client does not receive the emit.
Below is an example of the code for the application:
from flask import Flask
from flask_socketio import SocketIO
import eventlet
from celery import Celery
import time
eventlet.monkey_patch(socket=True)
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret'
socketio = SocketIO(app, async_mode='eventlet', logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379' )
celery = Celery(app.name, broker='redis://127.0.0.1:6379')
celery.conf.update(app.config)
#app.route('/')
def home():
return 'Hello World!'
#socketio.on('connect')
def connect():
print('Client connected, calling celery task...')
celeryTask(1,2)
#celery.task()
def celeryTask(x,y):
print('Celery task called!')
local_socketio = SocketIO(app, logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379')
while True:
local_socketio.emit('add', {'data': x+y})
time.sleep(60)
if __name__ == '__main__':
socketio.run(app, debug=True)
Any help would be greatly appreciated!
The socketio instance that you are using in your Celery task should not be initialized with the app instance from Flask. This isn't a web server, is just an auxiliary emitter.
#celery.task()
def celeryTask(x,y):
print('Celery task called!')
local_socketio = SocketIO(logger=True, engineio_logger=True, message_queue='redis://127.0.0.1:6379')
while True:
local_socketio.emit('add', {'data': x+y})
time.sleep(60)
If that does not work, you will need to add logs to your question, as that provide more clues.
Maybe try to put #celery.task() before #socketio.on('connect'). That might help.
I want to run two websockets on in my program, where one communicates to my Raspberry Pi (via websockets) and the other one to my browser via Flask-SocketIO.
Everything runs fine, when I run them in two different python files.
But I can't get them to run in the same file.
This is the Flask-SocketIO Server:
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
#app.route('/hello')
def hello():
return 'Hello'
def main():
print("abc")
if __name__ == '__main__':
socketio.run(app, debug=True, port=5000)
print("defg")
Output:
* Restarting with stat
* Debugger is active!
* Debugger PIN: ...
(12444) wsgi starting up on http://...
Noting gets printed.
This is the websockets server: (You can find that piece of code when googleing OCPP Python on mobilityhouses github)
import asyncio
import logging
import websockets
from datetime import datetime
from ocpp.routing import on
from ocpp.v201 import ChargePoint as cp
from ocpp.v201 import call_result
logging.basicConfig(level=logging.INFO)
class ChargePoint(cp):
# One function was left out to decluster the question
async def on_connect(websocket, path):
try:
requested_protocols = websocket.request_headers[
'Sec-WebSocket-Protocol']
except KeyError:
logging.info("Client hasn't requested any Subprotocol. "
"Closing Connection")
if websocket.subprotocol:
logging.info("Protocols Matched: %s", websocket.subprotocol)
else:
# In the websockets lib if no subprotocols are supported by the
# client and the server, it proceeds without a subprotocol,
# so we have to manually close the connection.
logging.warning('Protocols Mismatched | Expected Subprotocols: %s,'
' but client supports %s | Closing connection',
websocket.available_subprotocols,
requested_protocols)
return await websocket.close()
charge_point_id = path.strip('/')
cp = ChargePoint(charge_point_id, websocket)
await cp.start()
async def main():
server = await websockets.serve(
on_connect,
'0.0.0.0',
9000,
subprotocols=['ocpp2.0.1']
)
logging.info("WebSocket Server Started")
print("456")
await server.wait_closed()
if __name__ == '__main__':
asyncio.run(main())
print("123")
Output
INFO:root:WebSocket Server Started
456
I tried pasting it all in the same document and just doing this:
if __name__ == '__main__':
asyncio.run(main())
print("123")
socketio.run(app, debug=True, port=5000)
print("456")
But this just runs the first asyncio.run(main()) and doesnt print 123 etc.
If I switch it up, it will also just run the first .run and than stop.
I tried threading, but that had the same results.
Does anyone know how I can run these two at the same time?
I need to run some code after waking up a webserver. In the example it's "start_my_thing()".
import asyncio
import sys
import uvicorn
import socketio
#sio.on('connect')
async def test_connect(sid, environ):
pass
app = socketio.ASGIApp(sio, static_files={
'/': 'app.html',
'/static/': 'static/',
})
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8080)
start_my_thing()
The line with start_my_thing() will never be reached out because uvicorn.run() starts infinite loop.
But is there any trick to run some code when a webserver is already accessible?
I have a Flask script which creates a website and prints some data dynamically. - The data which it prints should come from another python script.
The current problem that I'm facing is that if I put the line that executes the python script before the line that executes the Flask app, it will run the Python script without running Flask; and vice versa.
Python script:
import websocket
from bitmex_websocket import Instrument
from bitmex_websocket.constants import InstrumentChannels
from bitmex_websocket.constants import Channels
import json
websocket.enableTrace(True)
sells = 0
buys = 0
channels = [
InstrumentChannels.trade,
]
XBTUSD = Instrument(symbol='XBTUSD',
channels=channels)
XBTUSD.on('action', lambda msg: test(msg))
def test(msg):
parsed = json.loads(json.dumps(msg))
print(parsed)
XBTUSD.run_forever()
Flask script (NB: price should be the variable 'parsed' from the other script):
# Start with a basic flask app webpage.
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from random import random
from time import sleep
from threading import Thread, Event
import requests, json
import time
__author__ = 'slynn'
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
#turn the flask app into a socketio app
socketio = SocketIO(app)
#random number Generator Thread
thread = Thread()
thread_stop_event = Event()
class RandomThread(Thread):
def __init__(self):
self.delay = 1
super(RandomThread, self).__init__()
def randomNumberGenerator(self):
while not thread_stop_event.isSet():
socketio.emit('newnumber', {'number': parsed}, namespace='/test')
sleep(self.delay)
def run(self):
self.randomNumberGenerator()
#app.route('/')
def index():
#only by sending this page first will the client be connected to the socketio instance
return render_template('index.html')
#socketio.on('connect', namespace='/test')
def test_connect():
# need visibility of the global thread object
global thread
print('Client connected')
#Start the random number generator thread only if the thread has not been started before.
if not thread.isAlive():
print("Starting Thread")
thread = RandomThread()
thread.start()
#socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app)
Using import:
Wrap what the python script (e.g. website_generator.py) is generating into a function.
Place it in the same directory as your app.py or flask.py.
Use from website_generator import function_name in flask.py
Run it using function_name()
You can use other functions such as subprocess.call et cetera; although they might not give you the response.
Example using import:
from flask import Flask
import your_module # this will be your file name; minus the `.py`
app = Flask(__name__)
#app.route('/')
def dynamic_page():
return your_module.your_function_in_the_module()
if __name__ == '__main__':
app.run(host='0.0.0.0', port='8000', debug=True)
try this:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def run_script():
file = open(r'/path/to/your/file.py', 'r').read()
return exec(file)
if __name__ == "__main__":
app.run(debug=True)
I have a pyhton-socketio server that servers on port 8000 and handles connections perfectly. I want to emit the following emit message every 2 seconds. When emitting the client doesn't receive a message. If I emit from where I point out in the comment it works perfect.
How can I edit this to be able to emit from inside the while loop successfully?
The python server code:
import socketio
import eventlet
import threading
import time
from flask import Flask, render_template
sio = socketio.Server()
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
# as a decorator:
#sio.on('connect')
def connect_handler(sid, environ):
print('IP->' + environ['REMOTE_ADDR'])
# If I emit here it works e.g. sio.emit('status-update', {'core0_in': 8, 'core1_in': 12,'cpu_usage_in': 5, 'users': 7})
#sio.on('disconnect')
def disconnect(sid):
print('disconnect ', sid)
class Server(threading.Thread):
def __init__(self, thread_id):
threading.Thread.__init__(self)
self.threadID = thread_id
def run(self):
print("Starting " + self.name)
serve()
def serve():
if __name__ == '__main__':
global app
# wrap Flask application with socketio's middleware
app = socketio.Middleware(sio, app)
# deploy as an eventlet WSGI server
eventlet.wsgi.server(eventlet.listen(('', 8000)), app)
server_thread = Server("Server-thread")
server_thread.start()
while True:
print("Emitting...")
sio.emit('status-update', {'core0_in': 8, 'core1_in': 12,'cpu_usage_in': 5, 'users': 7}) # when emitting here the client doesn't receive anything
time.sleep(2)