Send multiple requests to Flask API - mysql

I ran into this exception while I was working with Angular to show my Flask API REST data which is deployed on an nginx server:
{ "error": "(_mysql_exceptions.ProgrammingError) (2014, \"Commands out of sync; you can't run this command now\") [SQL: 'SELECT ......" }
This exception is caused by this function that sends two requests at the same time to the database:
ngOnInit() {
let url = this.baseUrl + `/items/${this.id}`;
this.httpClient.get(url).subscribe((data : Array<any>)=> {
this.ItemToEdit = data;
});
let url = this.baseUrl + '/products'
this.httpClient.get(url);
}
I use SQLAlchemy in the API with Mysql database. I thought that If I add a pool connection it will be resolved but It didn't, I still get same exception:
engine = create_engine(connection_string, pool_size=20, max_overflow=0)
What exactly should I do to handle this ?
Is there anything else to set on the server side to make this function work without getting an exception ?
EDIT:
Is this an observable handling problem or it can be fixeb by using Gunicorn with nginx on server side ?

Using Gunicorn solved my problem.
I launch the application this way:
gunicorn -b 0.0.0.0:5000 --workers=5 myapi:app
And I don't see the error anymore even with 3 mysql requests in ngOnInit

Related

How to Connect to MySQL database using Swift-Nio

I used node js to connect mysql server and it is working good. I used the API developed in node js and used in the local macOS app. The problem is querying the data from mysql database on workbench is fast but in node js API it is very delay. So, I've to connect mysql db directly from my macOS app.
I installed mysql from here: https://www.mysql.com/downloads/
Also I am using mysql work bench to test the connection and It is connecting without any problem. So, In system preference I've an mysql extensions to start and stop mysql server. In the extension I can see the root folder locate in /usr/local/mysql
I created new project and added swift package of mysql-nio from here
In that repository their API document link is broken https://api.vapor.codes/mysql-nio/master/MySQLNIO/ But I found the correct link https://api.vapor.codes/mysql-nio/main/MySQLNIO/MySQLConnection/
By using that link, I tried to connect with below code,
//https://github.com/vapor/mysql-nio
import Cocoa
import MySQLNIO
class ViewController: NSViewController {
private var group: EventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 4)
private var eventLoop: EventLoop {
return self.group.next()
}
override func viewDidLoad() {
super.viewDidLoad()
do{
let socketAddress = try SocketAddress(ipAddress: "127.0.0.1", port: 3306)
let logger = Logger(label: "MyLogger")
let connection = MySQLConnection.connect(to: socketAddress, username: "root", database: "MyDB", password: "3$#sdRt34#sdf$dja2", tlsConfiguration: nil, serverHostname: nil, logger: logger, on: eventLoop)
connection.whenComplete { result in
switch result{
case .success(let connection):
print("Success! Status closed: \(connection.isClosed)")
case .failure(let error):
DispatchQueue.main.async {
NSApp.presentError(error)
}
}
}
}catch{
NSApp.presentError(error)
}
}
}
Error:
The operation couldn’t be completed. (NIOCore.IOError error 1.)
I added App Transport Security Settings -> Allow Arbitrary Loads to YES in the xcode project info
I don't know How to resolve this issue. Can you please help me to resolve this issue? Thank you!
In the app sandbox, you'll need to enable "Outgoing connections (client)" and then it should work.
Unfortunately, because of a Swift bug the error printout of Swift errors isn't actually useful if you do localizedDescription. To see the actual error, you'd need to put something like a print(error) in your error handler.
See how in the screenshot below, the print(error) prints actually useful text like connect(descriptor:addr:size:): Operation not permitted (errno: 1) if I don't check the "Outgoing connections (client)"?

ESP32 gives error on HTTP Post to Flask server

My goal is to post data to a Flask server. For this I have the following code running on a computer(Jupyter):
from flask import Flask
from flask import request
app = Flask(__name__)
#app.route('/postjson', methods = ['POST'])
def postJsonHandler():
print (request.is_json)
content = request.get_json()
print (content)
return 'JSON posted'
app.run(host='0.0.0.0', port= 8090)
On the esp I have the following function responsible for posting, Right now it is just for testing , I will further the functionality later on.
//Posts data to server
void post_to_server(String url)
{
HTTPClient http;
// Prepare JSON document
JsonObject root = doc.to<JsonObject>();
JsonArray pressure = root.createNestedArray("pressure");
JsonArray time = root.createNestedArray("time");
pressure.add("Pressure");
time.add("Time");
// Serialize JSON document
String json;
serializeJson(root, json);
// Send request
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(json); //Send the actual POST request
// Read response
Serial.print(http.getString());
if (httpResponseCode > 0)
{
String response = http.getString(); //Get the response to the request
Serial.println(httpResponseCode); //Print return code
Serial.println(response); //Print request answer
}
else
{
Serial.print("Error on sending POST: ");
Serial.println(httpResponseCode);
// Disconnect
http.end();
}
}
So here is the odd thing, when I call the function on a test server like this:
post_to_server("http://jsonplaceholder.typicode.com/posts");
It works and I get the following response on the Serial Monitor as expected:
{
"pressure": [
"Pressure"
],
"time": [
"Time"
],
"id": 101
But when I try to post to the Server running on my PC like this:
post_to_server("http://127.0.0.1:8090/postjson");
I get the following error:
0
[E][WiFiClient.cpp:258] connect(): socket error on fd 54, errno: 104, "Connection reset by peer"
Error on sending POST: -1
I cant really make sense of this so I came here. I would appriciate any help. I also get the following when I test on Postman:
post_to_server("http://127.0.0.1:8090/postjson");
This will never work on your ESP32.
127.0.0.1 is the "loopback address" - the same as the name localhost. It's shorthand meaning "this computer".
When you use this with a program you run on your Windows machine, the program will attempt to connect to the Windows machine.
When you use this with your ESP32, it means connection to the ESP32.
You need to use the IP address associated with your Windows machine's network connection, whether ethernet or WiFi. 127.0.0.1 will not work.

Node Lambda: MySQL query never runs

Testing with Postman, I'll try to make this as clear as possible, please advise if this is not making sense.
I have a Lambda that uses MySQL RDS database on AWS and works fine locally when accessing the database on AWS. After successfully getting a JWT from an auth endpoint I try to hit the login endpoint and I get a 502 Bad Gateway. Using the CloudWatch logs I can trace the failure to right before the login query runs. I've confirmed that my MySQL config is correct and that I have a connection to the database. The lambda and the database are in the same region DB: us-east-1f, lambda: us-east-1.
I've confirmed the OPTIONS and POST request methods for this endpoint both are set up with CORS enabled in the API Gateway. I'm using my serverless.yml to set cors: true on all the endpoints even though I'm using app.use(cors()) in my index file.
The error message for the 502 is, {"message": "Internal server error"}
Here is the point of failure in my code:
'use strict';
const mysql = require('./index');
module.exports = {
loginSql: async (email, password) => {
// MAKES IT HERE AND THE PARAMS ARE CORRECT
try {
console.log('IN TRY %%%%%%%%%%%%%%');
// SEEMS TO DIE HERE
const results = await mysql.query({
sql: `SELECT
id, first_name, last_name, email
FROM users
WHERE email = ?
AND password = ?`,
timeout: 50000,
values: [email, password],
});
// NEVER MAKES IT HERE /////////
console.log('QUERY RAN %%%%%%%%%%%%');
mysql.end();
if (results.length < 1) return false;
return results;
} catch (error) {
// DOESN'T THROW ERROR
console.log('LOGIN DB ERROR', error);
throw new Error('LOGIN DB ERROR THROWN', error);
}
},
};
I just created the exact same use case in that I have a LAMBDA function written in Java querying data from a MySQL RDS instance. It works perfectly.
Here is your issue:
To connect to the RDS instance from a Lambda function, you must set the inbound rules using the same security group as the RDS Instance. For details, How do I configure a Lambda function to connect to an RDS instance?.

#feathersjs/socketio-client connection timeout. Why?

I maked follow:
feathers g app # with socket and rest
feathers g service # todos & NeDB
npm start
and simple client for this. I copy code from documentation
https://docs.feathersjs.com/api/client/socketio.
const feathers = require('#feathersjs/feathers');
const socketio = require('#feathersjs/socketio-client');
const io = require('socket.io-client');
const socket = io('http://localhost:3030');
const app = feathers();
app.configure(socketio(socket));
app.service('todos')
.on('created', message => console.log('New message created', message));
app.service('todos').find().then(r => {
console.log(r)
}).catch(e => console.log('error',e))
app.service('todos').create({
title: 'A message from a REST client'
});
this client code get me timeout errors for find() and create() methods
But if I make POST request by CURL, I have onCreated message in console
Why I got errors on create() and find() calls?
UPDATE:
I maked git repo for easy reproduce this problem
https://github.com/tolyanor/feathersjs-error
UPDATE2:
I change autogenerated file src/app.js like in feathers example chat application https://github.com/feathersjs/feathers-chat/blob/master/src/app.js
Now I CAN call service method create on client, but CAN NOT receive onCreated message. So, this code
app.service('/todos')
.on('created', message => console.log('New todos created', message));
never calling
You are using a Feathers v3 client with a Feathers v2 server. Old clients will be backwards compatible with new servers but not the other way around. Follow the migration guide to upgrade your server or generate a new application using #feathersjs/cli (feathers --version on the command line should show v3.5.0 or later).

express send incomplete response json data

The version info:
"express": "~4.15.2",
"express-session": "^1.15.5",
I use this code send large json data to client:
router.get('/exportAllData',function(req,res,next){
async function getData(){
let sql="SELECT * FROM int_information "
let rows=await query(sql);
let data=await JSON.stringify(rows);
return JSON.parse(data);
}
getData().then(data=>res.send({flag:1,data:data})).catch(error=>{
res.send({flag:0,err:error});
});
})
I tried wget localhost on server and can get full data,
But the remote client got broken json data and different each time refresh.
Where is the problem? Seems response closed before get all data.
This is an open bug in node js 8.x, I use 7.10 solved this problem.
Or you can set server.keepAliveTimeout=30000 in app.js to extend timeout setting