Create Asynchronous Flask App Using Gunicorn-Gevent

The problem I’ve faced when trying to move machine learning to production is machine learning system usually takes time to compute or give a result. If you create an API (machine learning API) in super computer server or your API will be accessed by a very few people, you can ignore this problem but unfortunately I need this.

Asynchronous API

The first solution in my mind is to create an asynchronous API. What is it? The simplest explanation about asynchronous is from stackoverflow: You are cooking in a restaurant. An order comes in for eggs and toast.

  • Synchronous: you cook the eggs, then you cook the toast.
  • Asynchronous, single threaded: you start the eggs cooking and set a timer. You start the toast cooking, and set a timer. While they are both cooking, you clean the kitchen. When the timers go off you take the eggs off the heat and the toast out of the toaster and serve them.
  • Asynchronous, multithreaded: you hire two more cooks, one to cook eggs and one to cook toast. Now you have the problem of coordinating the cooks so that they do not conflict with each other in the kitchen when sharing resources. And you have to pay them.

So, I need to make my API able to run many request at the same time. (Another explanation : from codewala)

How to implement it?

There’s plenty way to implement it. But I think I’ve found the simplest solution, especially if you’ve created an API using Flask: Using Gunicorn and Gevent. First, you should install Gunicorn and Gevent, you can install it via pip:

pip install gunicorn
pip install gevent

To run your API using gunicorn the only difference you should make is the command when you need to run the app. Usually if you have a Flask app you run it with a command below:


if you are using gunicorn then you should run it with a command below:

gunicorn myapp:app

`myapp` is your file name, and `app` is your flask app variable. After the app is running you can access it on http://localhost:8000 (notice the default port is changing, if you’re using Flask, you run your app in port 5000 but if using gunicorn you run it in port 8000).

It’s not finish yet, when you run using the command above, you just run it synchronously. Using Gunicorn, you can run it asynchronously without change any line of your code! just run it with the command below:

gunicorn -k gevent -w 5 myapp:app

-k option makes gunicorn run the program asynchronously using gevent service (there are more services which provide async like eventlet). -w option makes gunicorn create workers with specified number (in this example 5) (other reference: Workers).

And Congratulation, you just run your flask app asynchronously! if you want to test your API you can use apache benchmark. it can access your app many times simultanously. The example command is below:

ab -n 200 -c 100 http://localhost:8000/myapp

If you see the result, you will see the difference in number of request per second between you run it sync and async.

Thank you πŸ™‚ I hope this note is helpful for me or anybody.

Reinforcement Learning Implementation

This is one of the results from my “Holiday Sprint” last month, get in touch with Reinforcement Learning!. The last two months I’m trying to implement algorithms from Sutton’s Book Reinforcement Learning: an introduction. It still in progress (I hope it will finish this semester), if you want to check the source codes you can see in my github repo : reinforcement-learningΒ  and I’m glad to hear your suggestions πŸ™‚

My learning path to learn reinforcement learning

So far, this is my learning path to learn the basic of reinforcement learning

  1. Read the Sutton’s and Barto’s Book, Reinforcement Learning: an Introduction. I read a few chapter, then I got confused a lot with many mathematical term 😐 so..
  2. I started to watch a lot of videos from Udacity Reinforcement Learning Course. It’s really fun watch the course there πŸ™‚ they successfully simplified a lot of topics
  3. Reinforcement Lecture from David Silver on Youtube. These videos help me to understand difficult term from Sutton’s book.

Simple Tutorial Python, Boto3, and AWS S3

In the past few weeks, I’ve horrible experience to find “good” tutorial about how to use Boto3 in Amazon Web Service (AWS). Boto3, not like Boto2, has poor quality documentation. So I create this simple tutorial as reminder to myself and I hope it will help someone out there.


The AWS SDK for Python. It simply to said, if you have a python apps and you want it to access AWS features, you need this.

Installation & Setup

Installation is very clear in python documentation and for configuration you can check in Boto3 documentation just using pip:

$ pip install boto3

after install boto3 you can use awscli to make it easier setup credentials, install it using pip too:

$ pip install awscli

set your configuration using command below. You can get your access key and secret key in IAM console:

$ aws configure

Create A Simple App

Let’s create a simple app using Boto3. We will create a simple app to access stored data in AWS S3. This app will write and read a json file stored in S3.

1. Prepare Your Bucket

First, you need to create a bucket in your S3. You can create bucket by visiting your S3 service and click Create Bucket button.


or using some code below:

import boto3
import json

data = {"HelloWorld": []}
s3 = boto3.resource('s3')

If you confuse what is bucket and how it works, this one have nice explanation.

2. Write JSON File

Next part is how to write a file in S3. The code below will create a json file (if it doesn’t exist, or overwrite it otherwise) named hello.json and put it in your bucket.

import boto3
import json

data = {"HelloWorld": []}
s3 = boto3.resource('s3')
obj = s3.Object('my-bucket','hello.json')

If you want to put it on specific path, you can change the line

obj = s3.Object('my-bucket','hello.json')


obj = s3.Object('my-bucket','my-path/hello.json')

Run it, and if you check your bucket now you will find your file in there.

3. Read JSON File

Last but not least, if you want to read your file, you can use get() function. The code below will read your hello.json file and show it on screen. File body/content will be read as string by default.

import boto3

data = {"HelloWorld": []}
s3 = boto3.resource('s3')
obj = s3.Object('my-bucket','hello.json')
data = obj.get()['Body'].read()
print data


I think that’s all, a simple tutorial using boto3 to read-write file in Amazon Web Service S3. Hope it helps πŸ™‚

Manage Your Life Using Trello

I’m not exaggerating when I said “manage your life”, because it really happens. If you are a multitasker with many works which should be done at the same time or a man who always forget to do something during the day, you need to manage your life properly.

First time I used Trello was when I had a job in, a successful start-up about cultivating fish in Yogyakarta, Indonesia. We set our Trello every week; made a todo list, set the target, reported the last week progress and estimated the difficulties of our tasks for next week. At that time, I thought Trello was only used when you have a team and a project needs to be done. But I was wrong, Trello can be used for manage everything, even your life activities!

So, what is Trello? is a project management service provider which helps you organize your projects. Like the homepage said, “Trello’s boards, lists, and cards enable you to organize and prioritize your projects in a fun, flexible and rewarding way.” And yup! “Boards”, “Lists”, and “Cards” are the key elements of Trello. And you’ll see how amazing it is.

My trello’s board for finishing my thesis

Trello in my experiences

I’ll try to explain to you about Trello based on my experiences. So, like I said, there are three important keys you should know about Trello:


As you can see in the image above, it is a screenshot of my “Board”. It was created to finish my thesis last year. You can have multiple “Boards” to manage different projects in one account. But for the “usage” is up to you, because I have one big “Board” which filled with all of my projects, then each project also has a special “Board” for them. So my big board tries to manage timeline between projects in my life, and for specific detail of each project I can check in their “Board”.


In the screenshot above you can see there are 5 “Lists” which I used in that board. “List” categorizes the cards you are using for specific set of tasks. Back in my workday in every “List” (it was called “Backlog”) served a specific purpose/objective. For example, there were “Backlogs” for improving web, widen business things, create documentation, etc. But in my experience for organizing things, I just create 4 “Lists”: “Icebox” (a waiting list), “Doing” (in progress list), “Done”, “Skipped” (canceled tasks)

My “Board” for “Holiday Sprint” event this semester, only has 4 “Lists”


A “Card” is a representation of an objective you should complete.Β  You can put many things on “Card”: image, labels, files, link, checklist, or a bunch of notes!

My “Cards” always have a note for each “things I should remember”

Then how I manage things using this?

This is the best part, Trello doesn’t dictate you to “how to use it”. You can use it the way you see fit. The principle is: Move your “Card”, whether across “Lists” or “Boards”, it’s up to you. In my experience, first I’ll write all my tasks which I can think of at that time, give some initial notes (may be a deadline too), give a label (actually I use this label to make it more colorful ._.), then put them in the “Icebox”. At the beginning of day/week, I move some cards from Icebox to “Doing”, those are tasks I should focus on that day/week. If I need, I’ll write a note if I need, and then after I’m done, I put it on “Done”. Just as simple as that. But once again.. everything is up to you on how to use it πŸ™‚


I think it is all of my story and experience in using Trello the last year. After more than one year of using it, I feel it really helps me a lot to focus and remember my tasks. And I hope it will help you too πŸ™‚ If you have any suggestions or other tips, let me know!

EDIT : Thanks to Fikri for correcting my grammar πŸ˜‰

How Do I Learn Neural Network (and Deep Learning)

I can’t remember,.. in one semester(?)

First, I am nobody in this topic, please don’t hesitate to give better suggestion. This is actually just a “documentation” about my experience when I work on my undergraduate thesis and answer to many people who ask me how I learn neural network and deep learning last year. Once again, please don’t hesitate to discuss and give better suggestion about this topic, or.. correct my English if I made some mistakes πŸ™‚


Okay, before I tell you my story, I’ll tell you about my background when I started to learn neural network. I wrote this section only have one intention, to make you easy measure what you need, nothing else.

  1. I have familiarity with some of mathematical symbols, like how to read sigma symbol, derivative symbol and many others (but not every symbols).
  2. I have familiarity with basic math operation like derivative function, algebra, matrix and vector operation (but only “the basic”).
  3. I’ve never coded in python, but I’ve been using C++ around 2-3 years and I used to codeΒ  Matlab style programming language in some project before (and thanks, this knowledge is very helpful)
  4. I studied the Introduction of Artificial Intelligence at the fourth semester in college, but I won’t said I did it well πŸ™

After a month googling randomly, I started to make a list what should I learn, the order of the list below it’s justΒ the best order in my opinion. In my experience I jumped many times over the topic because at that time, I didn’t know what I didn’t know πŸ™

If you want to learn about Deep Learning, lets begin with: What is Machine Learning?

Then start to learn about Neural Network

Let’s do some math, Backpropagation?

I suggest you to fully-understand this algorithm in order to give some intuitions to help you in model tuning or error handling when you start implement the neural network

  • Easy start : I watched again the neural network demistifyed, and slowly got the intuition how backpropagation work, I followed and tried the math step-by-step
  • Watch the 4th week on Coursera Introduction to Machine Learning
  • Try the example of how backpropagation works :
  • Read carefully the second chapter from
  • And if you start thinking they have different formula / calculation / interpretation, start to prove that you are wrong. I started to compare all the calculations to understand more

Python for machine learning?

So what is Deep Learning?

What is CNN (I use CNN in my undergraduate thesis)?

Backpropagation in CNN?

I don’t know it is necessary or not, but I learned it. I think if you understand the basic of backpropagation it’s enough. But if you are curious how backpropgation through max pooling or convolution layer, it does not hurt to learn it.

And…great, It’s done, That’s all my references that I used when I did my undergraduate thesis last year, I hope it will be useful for someone :/ or at least for me in the future πŸ™‚

EDIT : Thanks to Farah for correcting my grammar πŸ™‚