Replies: 1 comment
-
You can't use eventlet if the program performs heavy CPU calculations because these block. Eventlet is an asynchronous framework. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I have creaed a sample flask socketIO server app which has a custom event that takes atleast 10 seconds to finish job and respond, with options async_mode="eventlet" and async_handlers=True
ref:
socketio= SocketIO(app,async_handlers=True,async_mode="eventlet")
when tried to connect using sample c# client(s) the following are my observations
First client instance was able to establish connection immediately and custom event got triggered and processing started
when second client is run, during first client processing it waited for first client to finish the processing , then the second client got connected and custom event is initiated
when I run multiple clients one by one, only first client is connected and all other clients are not even connecting and waiting for the server,
once the first client processing is done, most of the clients are getting connected all at once, but also observed few clients are still showing blank as they did not connect to the server,
for all the clients that are connected server, their requests are processed the results are sent back to clients one by one and in some instances, few connected client are not getting response, but they kept on ping pong the server
But my requirement is, i want to have a flask-SocketIO application that contains a custom event, which takes some time (may be 2 to 3 mins) to process and return results back to the clients concurrently
I want to understand if this is possible achieve this with flask-socketIO, since we are in phase of tech stack decision making
additional details are as follows:
Flask socket IO server:
import asyncio
import eventlet
loop = asyncio.get_event_loop()
eventlet.monkey_patch()
from flask import Flask
from flask_socketio import SocketIO,send,emit
import time
from datetime import datetime
import random
app= Flask(name)
app.config['SECRET_KEY']="mysecret"
socketio= SocketIO(app,async_handlers=True,async_mode="eventlet")
@app.route('/')
def index():
return "Flask Socket IO server started 3.24 pm";
def complex_calculation():
print('Started calculating...')
start = time.time()
for x in range(20000000):
x**2
print('complex_calculation: ', time.time() - start)
@socketio.on_error()
def error_handler(e):
print( e)
#this fires
@socketio.on("connect")
def connect():
print( "successfully user connected to the server")
@socketio.on('myevent')
def myevent(msg):
print('message from client : '+msg)
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print("Current Time =", current_time)
resul=complex_calculation()
print("result from complex calc ")
emit("myevent"," hey client, this is response from server...."+msg)
if name=='main':
socketio.run(app,host='127.0.0.1',port=5000,log_output=True)
C# Client
using SocketIOClient;
using SocketIOClient.JsonSerializer;
using SocketIOClient.Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
namespace SoketIODotnetClient
{
class Program
{
static DateTime startTime = DateTime.Now;
static Uri uri = new Uri("http://127.0.0.1:5000/");
}
}
Thanks in advance,
kthinnal
Beta Was this translation helpful? Give feedback.
All reactions