Category: Python

2020-09-15T12:09:29Z

Ignore All Web Performance Benchmarks, Including This One

Posted by Miguel Grinberg under Python.

A couple of months ago there was an article titled Async Python is Not Faster making the rounds on social media. In it, the author Cal Paterson made the point that contrary to popular belief, asynchronous web frameworks are not only "not faster" than their traditional synchronous counterparts, but they are also slower. He supports this by showing the results of a fairly complete benchmark that he implemented.

I wish things were as simple as this author puts them in his blog post, but the fact is that measuring web application performance is incredibly complex and he's got a few things wrong, both in the implementation of the benchmark and in his interpretation of the results.

In this article you can see the results of my effort in understanding and fixing this benchmark, re-running it, and finally arriving at a shocking revelation.

2 comments

2020-09-08T14:49:01Z

Sync vs. Async Python: What is the Difference?

Posted by Miguel Grinberg under Python.

Have you heard people say that async Python code is faster than "normal" (or sync) Python code? How can that be? In this article I'm going to try to explain what async is and how it differs from normal Python code.

20 comments

2020-09-01T09:24:09Z

How to Deploy a React-Router + Flask Application

This is the third article in my "React + Flask" series, in which I discuss applications that combine a Flask API server with a React single-page application. This time I'm going to show you how to work with the popular React-Router library for React, and in particular how this library affects the production deployment of the application.

This is the third article in my "React + Flask" series. Make sure you read the first and second parts, as this part builds on the project built up to this point.

2020-08-06T20:40:09Z

Video: How To Fix an Internal Server Error in Flask

In this beginner level video I explain what steps you need to take when you get an Internal Server Error in your Flask application.

2020-07-30T21:55:00Z

Fixing ALTER TABLE errors with Flask-Migrate and SQLite

If you've done any work with SQLite databases you surely know that this database is very limited in terms of making changes to the database schema. When working with a migration framework such as Flask-Migrate, it is common to end up with migration scripts that fail to upgrade or downgrade just because they need to remove or modify a column in a table, something that SQLite does not support.

In this article I'm going to discuss this limitation of the SQLite database, and show you a workaround that is specific to Flask-Migrate and Alembic.

1 comment

2020-07-20T10:29:00Z

Run Your Flask Regularly Scheduled Jobs with Cron

A common need of web applications is to have a periodically running task in the background. This could be a task that imports new data from third party sources, or maybe one that removes revoked tokens from your database once they have expired. In this and many other situations you are faced with the challenge of implementing a task that runs in the background at regular intervals.

This is a pattern that many people ask me about. I've seen implementations that are based on the APScheduler package, on Celery, and even homegrown solutions built inside a background thread. Sadly none of these options are very good. In this article I'm going to show you what I believe is a very robust implementation that is based on the Flask CLI and the cron service.

14 comments

2020-07-08T11:41:50Z

Handling File Uploads With Flask

A common feature in web applications is to let users upload files to the server. The HTTP protocol documents the mechanism for a client to upload a file in RFC 1867, and our favorite web framework Flask fully supports it, but there are many implementation details that fall outside of the formal specification that are unclear for many developers. Things such as where to store uploaded files, how to use them afterwards, or how to protect the server against malicious file uploads generate a lot of confusion and uncertainty.

In this article I'm going to show you how to implement a robust file upload feature for your Flask server that is compatible with the standard file upload support in your web browser as well as the cool JavaScript-based upload widgets:

Basic file upload form

40 comments

2020-06-29T17:00:56Z

Access Localhost From Your Phone Or From Anywhere In The World

Sometimes it is useful to quickly access your Flask application running on localhost from another device or location for testing purposes. In this article I'll show you how to use the pyngrok package to provision a temporary public URL for your application that works from your phone or from anywhere in the world!

6 comments

2020-05-29T22:13:32Z

Video: Using Trailing Commas in Python

In this short video I comment on the benefits of using "trailing commas" when defining data structures in Python.

2020-05-07T18:00:47Z

Video: Setting Up Vim to Work with Python Applications

In this short video tutorial I demonstrate my Vim editor setup for working with Python applications.

Do you want to try this out? Download my .vimrc file! If your tagbar does not work, install Exuberant Ctags (use brew install ctags or sudo apt-get install ctags, or download from http://ctags.sourceforge.net/)

15 comments