ud303-concurrency-bookmark-server

What's the problem?

The bookmark server can't handle over 1 request.

If user try to use bookmark server URI (ex: "https://ud303-loukei-bookmarksever.herokuapp.com/") as bookmark, server will try to make requests for himself.

Demo

def __check_longuri(self, longuri:str)->bool:
    "Check longuri, if uri exist, return true"
    try:
        res:requests.Response = requests.get(url=longuri)
        return res.status_code == 200
    except requests.RequestException:
        return False
    except:
        return False

Since our server can't handle over 1 request, the server will crash instantly.

How to fix it?

#TODO

  1. how to use thread to solve concurrency problem?
  2. What is ThreadingMixIn? How does it work with http.server.HTTPServer?
import threading
from socketserver import ThreadingMixIn

class ThreadHTTPServer(ThreadingMixIn, http.server.HTTPServer):
    "This is an HTTPServer that supports thread-based concurrency."

Use ThreadHTTPServer instead of HTTPServer

if __name__ == '__main__':
    port = int(os.environ.get('PORT', 8000))
    server_address = ('', port)
    httpd = ThreadHTTPServer(server_address, Shortener)
    httpd.serve_forever()

The OS concepts

The program

Program means a couple of files stored in computer disc. For example, if you installed Internet Explorer, but not using it now, then it's a program, an inactive data in disc.

The process

The process is opposed to the program, which means the OS is running the program and distributing resources like memory and CPU for it, not data stored in the disc.

The thread

The thread is a small unit that OS can schedule, each process can have more than one thread, every thread in same process share the resource of the process.

For example, in modern browser, each page use different thread, but they share the resource which OS distribute to our browser.

Another example is the process is like a news paper, the thread is like a person who's reading it, when you reading the economic article, your wife can read the sports article. Each person use the different part of resource, but when they want to access the same article, it will cause race condition.

Concurrency

Concurrency means we split our job into several different task, they share the same resource at a time. When CPU running a concurrency task, it picks one task, running it, then switch to another task, so it looks like those task are running parallel but not.

Why we need concurrency in this case?

#TODO

Reference