question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

0.5 second connection overhead

See original GitHub issue

I’m having some issues with my connection requests ever since I have followed the best practices defined in the docs:

Code in docs:

from flask import Flask
from peewee import *

database = SqliteDatabase('my_app.db')
app = Flask(__name__)

# This hook ensures that a connection is opened to handle any queries
# generated by the request.
@app.before_request
def _db_connect():
    database.connect()

# This hook ensures that the connection is closed when we've finished
# processing the request.
@app.teardown_request
def _db_close(exc):
    if not database.is_closed():
        database.close()

After this change, all handled requests have a 0.5 second overhead in response time.

screenshot 2019-02-17 at 16 04 04

I’m currently investigating and was wondering if this was something that could be somehow caused by Peewee’s handling/closing of new connections, or if it is something else.

Thanks for your help.

Will

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
NicolasCaouscommented, Feb 17, 2019

Your snippet is opening and closing a database connection at every request. There is an overhead time built into it (You can’t escape it). Maybe you should consider altering the way in which you manage connections. Something like:

Open a connection at startup.

On @app.before_request verify that the connection wasn’t closed by the database (or by any other reason). If it’s closed, open it again, otherwise, recycle the connection.

You may be interested in a pooled connection.

Edit

This is a bad advice, see @coleifer comment for clarification.

1reaction
NicolasCaouscommented, Feb 18, 2019

Yes, you can use a connection string to configure the pooled database connection.

http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#connect

The kwargs are parsed from the URL query parameters.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TCP Over IP Bandwidth Overhead - Packet Pushers
How long will it take to transfer a 100MB file over an IPSec tunnel running across a dedicated 100Mbps Ethernet link? 1 Second?...
Read more >
Understanding network overhead - Catalin Dumitru
The other half is network latency and the overhead of actually establishing a connection with your servers. In this blog post we will ......
Read more >
Connection Pooling - How much of an overhead is it?
A simple pool should be able to handle 100K allocations per second or more or about 10 micro-seconds. However, as soon as you...
Read more >
CS 356 Flashcards - Cram.com
0.5 -Mbps - >= 125 packets per second ... Using a TCP SYN spoofing attacker, the attacker aims to flood the table of...
Read more >
SSL Performance Overhead in MySQL - Percona
Test 2: Connection Time. For the second test, I wrote a simple Perl script to just connect and disconnect from the database as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found