Git Product home page Git Product logo

saeedghx68 / simple-twitter Goto Github PK

View Code? Open in Web Editor NEW
23.0 2.0 4.0 32 KB

A simple twitter is an application which enables users to register, post tweets and search by tag. Simple Twitter is written in sanic which uses uvloop in order to have async call.

Smarty 0.65% Dockerfile 0.38% Makefile 0.97% Python 89.22% Shell 8.78%
sanic python3 python37 uvloop asyncio twitter gino biolerplate simple-twitter sanic-framework

simple-twitter's Introduction

Simple-Twitter

A simple twitter is an application which enables users to register, post tweets and search by tag. Simple Twitter is written in sanic which uses uvloop in order to have async call.

In this project I used these technologies:

  • python version: 3.10.0
  • PostgreSQL (to store tweets)
  • redis (to cache tag and related tweets)
  • Sanic (as an asynchronous web API)

Sanic and uvloop makes asyncio fast. In fact, it is at least 2x faster than NodeJs, gevent, as well as any other Python asynchronous frameworks. The performance of uvloop-based asyncio is close to that of Go programs.

Run

clone the project and run with docker

cp .env.tpl .env
docker-compose build
docker-compose up -d # it will be exposed on port 2080

After this, you can work with API by Swagger. You can see swagger via http://127.0.0.1:2080/swagger url.

Run tests

docker exec -it [API DOCKER NAME] ENVIRONMENT=test pytest unittesting/

Test and Build

to run test or build application, we have make commands. So, to run tests:

Note: please install python3.10 before run make command

To test application

make test

Run app

make all

This command will install python3-pip and virtualenv. After that run it creates virtualenv and activates it.Then it installs requirements packages, and Finally run the following command: python -m sanic server.app --host=${host} --port=${port} --workers=${worker_count}

for example:

python -m sanic server.app --host=0.0.0.0 --port=2080 --workers=12

Why use redis?

Note: I choosed redis to cache tweets and tags. At the first, Simple twitter looks up for tweets when you search by tag on redis, If searched tag does not exist, it searches in pg and after that it inserts all related messages into redis to have them cached. I want to say, if the project grows up, you have to implement logic to store important tags and messages on some cache engine such as redis.

Why asyncio and sanic and uvloop?

asyncio & uvloop

The asyncio module, introduced by PEP 3156, is a collection of network transports, protocols, and streams abstractions, with a pluggable event loop. The event loop is the heart of asyncio. It provides APIs for:

* scheduling calls,
* transmitting data over the network,
* performing DNS queries,
* handling OS signals,
* convenient abstractions to create servers and connections,
* working with subprocesses asynchronously.

when you should consider sanic?

* high throughput
* micorservices
* smaller web apps
* it was super easy to get started

Architecture

uvloop is written in Cython and is built on top of libuv.

libuv is a high performance, multiplatform asynchronous I/O library used by nodejs. Because of how wide-spread and popular nodejs is, libuv is fast and stable.

uvloop implements all asyncio event loop APIs. High-level Python objects wrap low-level libuv structs and functions. Inheritance is used to keep the code DRY and ensure that any manual memory management is in sync with libuv primitives' lifespans.

It is safe to conclude that, with uvloop, it is possible to write Python networking code that can push tens of thousands of requests per second per CPU core. On multicore systems a process pool can be used to scale the performance even further.

uvloop and asyncio, combined with the power of async/await in Python 3.5+ , makes it easier than ever to write high-performance networking code in Python.

references

simple-twitter's People

Contributors

ilhanmercan avatar saeedghx68 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.