my net house


Run Flask in Parallel using ThreadPoolExecutor

from flask import Flask
from time import sleep
from concurrent.futures import ThreadPoolExecutor

executor = ThreadPoolExecutor(2)

app = Flask(__name__)

def run_jobs():
executor.submit(some_long_task2, 'hello', 123)
return 'Two jobs was launched in background!'

def some_long_task1():
print("Task #1 started!")
print("Task #1 is done!")

def some_long_task2(arg1, arg2):
print("Task #2 started with args: %s %s!" % (arg1, arg2))
print("Task #2 is done!")

if __name__ == '__main__':


OOPS and More OOPS in Python

Concurrency in Python or Natural way of life(Not yet completed POST)

There are various ways one can think about computing , Multiprocessing, Asynchronous, Multi-threading as well as “Parallel Processing” If I would talk about theoratical things I Would say we have to distribute our one particular task in various forms so multiple resources should be available for system to run things or in other way we can say multiprocessing is more of Programmer’s way of understanding the Flow of precess and sometimes rules according to theory does not assure that if one is providing multiple resources to process it will be FAST! it could be FAT! also.

Now let me start with very simple Example by taking following function as use case:

# Function that run multiple tasks
def get_response(url):
“””returns response for URL ”””
response = requests.get((url),verify=False)
return response.text

Now above function is simple enough that is getting one URL and returning response but if I have to pass multiple URLs but I want that get request to each URL should be fired at same time then That would be Asynchronous process not multiprocessing because in Multiprocessing Threads/Processes needs to communicate with each other but on the other hand in case of Asynchrounous threads don’t communicate(in Python because Python uses Process based multiprocessing not Thread Based although you can do thread-based multiprocessing in Python but then you are on your OWN 😀 😛 Hail GIL (Mogambo/Hitler)).

So above function will be like this as usual:

from multiprocessing import Pool
pool = Pool(processes=20)
resp_pool =,tasks)
URL_list = []
resp_pool =,tasks)

One thing you have to understand very carefully and that is GIL does not harm for i/o bound operations but yes when it comes to non-i/o bound operations in python You have Numpy,Scipy,Pandas,Cython where one can really release GIL and take full advantage of the code.

How to release GIL using Cython:
Although one can look for interesting features about GIL:

Intel has also provided Python Distribution that is helpful get speedups in Python but that would only be helpful for Machine-learning and Data-Science work. like worth to give it a Try:::)

Now there is one important thing you must need to care about when you are releasing GIL in Python.

You can also scratch your head many times by just reading/watching this one interesting presentation:

Although Numba is also there but make one thing for sure Use such tools only when your Operation is CPU bound not I/O bound because as I have stated that I/O bound operations don’t care about GIL.

Although you will find out that GIL is not just Python’s Problem:

I/O Bound:

The I/O bound state has been identified as a problem in computing almost since its inception. The Von Neumann architecture, which is employed by many computing devices, is based on a logically separate central processor unit which requests data from main memory,[clarification needed] processes it and writes back the results. Since data must be moved between the CPU and memory along a bus which has a limited data transfer rate, there exists a condition that is known as the Von Neumann bottleneck. Put simply, this means that the data bandwidth between the CPU and memory tends to limit the overall speed of computation. In terms of the actual technology that makes up a computer, the Von Neumann Bottleneck predicts that it is easier to make the CPU perform calculations faster than it is to supply it with data at the necessary rate for this to be possible.

In simple cases CPU is Faster and Memory is Slower.

Let’s make things more precise:
Sync: Blocking operations.
Async: Non blocking operations.
Concurrency: Making progress together.
Parallelism: Making progress in parallel.

Now Questions arises that do we need all those things together: I just found that Dask is much more Advanced and Promising that one should not ignore at all!!)


Running Multiprocessing in Flask App(Let’s Spawn) Hell Yeah

Ok It was going to be long time but Finally yeah Finally Able to do Process based multiprocessing in Python and even on Flask. 🙂 oh yeah! There are various recipes for Multiprocessing in this python but here you can only Enjoy with Flask.


from multiprocessing import Pool
from flask import Flask
from flask import jsonify
import ast
import pandas as pd
import requests

app = Flask(__name__)
_pool = None

# Function that run multiple tasks
def get_response(x):
“””returns response for URL list”””
m = requests.get((x),verify=False)
return m.text

def health_check():
“””returns pandas dataframe into HTML for health-check Services”””
resp_pool =,tasks)
table_frame= pd.DataFrame([ast.literal_eval(resp) for resp in resp_pool])
return table_frame.to_html()

if __name__==’__main__’:
_pool = Pool(processes=12) # this is important part- We
# insert production server deployment code
except KeyboardInterrupt:


One mintue read to one minute Manager

Get out more results in less time.

Autocratic VS Democratic:
Autocratic are result oriented and Democratic are happiness Oriented So we
need to be one minute Managers. 🙂

1. One minute Goal Setting:

Everyone should be knowing the goals of the company.
People must know what their roles are in the company.
Goals must not be more than 250 words.
Always review your Goals.

2. One minute Praising:

Give True Feedback.
Always praise immediately.
Share happiness and encourage your people.

3. One minute reprimand:

Immediately point people out for their mistakes.
Tell people how you feel about it.
Point out mistake but don’t criticize.
Be on the side of your people.

Look for the good things in the beginners and bad things in the experienced.
Share what you learn.
We don’t manage people, We manage behaviors.
Love your people and make sure they are also loving you back.
Define your problem grammatically. (What is happening and What you want to be happen.)

OOPS Design Principles(Completing course)

Writing a Method that takes one or few parameters as input and then returns various Outputs depends on the logic of the  function. This post will be dedicated on the OOPS design Principles and how those somehow complete Design principles for software development. All the principles in SOLID are somehow connected with each other and one can follows any one of those for Better development of code. Follow any principle(Say Open-Close) and after some time of developing your code you will find that you actually are following all the principles. 🙂 Because all these principles are developed by making one thing in mind and that is High-Quality Software development.


S(Solid Responsibility Principle): There should never be more than one reason to for a class to change.

O(Open-Close Principle): Classes(Modules or functions) should be open for extensions but closed for modifications.

***Features should be parameterized in the way that class can be overridden.

OCP suggests that some bug fixes should be viewed as an extensions instead of a modifications.

That clarifies the single responsibility principle. A class should be able to do only on kind of task. Kind means Reading data. Data could be read from one type of file or many type of files. For example there could be class name DataReader() and it could have many methods. csv_reader(), excel_reader(), db_reader(), txt_reader() so now single-responsibility of class is to ‘read data’ 🙂 any kind of data, by any means but read data. 🙂

Any class that we are going to write should be written as is it could be extended but no need to make new changes in the code of the class.

Interface Segregation Principle: (I from SOLID)

*** No client should be forced to depend on methods it does not use.
*** Class should be designed so collaborators should have narrowest interface.
*** If you are using Python Unit Test cases should be there because we are using Python!

Classes that implement interfaces should not be forced to implement methods they(classes) do not use. (Use small interfaces not FAT ones) Do not create fat interfaces.
We need different streams for read and write.

A client should be dependent on smallest set of Methods and attributes. The fewest methods or attributes.

Segregation defines parting the functions(Not programming functions) of Software into two or more different classes for ease of client to access the features.

D: Dependency inversion principle.

*** Applications should use abstract class code that lead to injection.
*** In language like Python we can use file to perform tasks like this one.

High level modules should not be dependent on low level modules. Abstractions should not be dependent on Details, Details should depend on abstractions. There should be one ‘Abstraction-Layer’ on each of the module so one would be able to test the behavior of classes without even looking at the classes.

L:(Liskov substitution Principle):

*** Behavior of sub-class should be as correct as behavior of super class.

When you will follow the Liskov substitution principle your classes will follow Open/Close principles implicitly.

Substitutability is a principle in object-oriented programming stating that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e. an object of type T may be substituted with any object of a subtype S) without altering any of the desirable properties of T (correctness, task performed, etc.).

Single responsibility principle:

** A class should have only one reason to change.

GRASP:>>>> General Responsibility assignment principle.
**Controller and Creation
**High Cohesion and Indirection
** High Cohesion:
Features those belong together.
High Cohesion are combined into single responsibility.
** Hard part is locating level of abstractions.

Why we should think about GRASP?:
Information Expert
Pure Fabrication
Low coupling
Protected variations


Lessons Learned from life

Complete Basics those just went out of my mind, No idea how those gone away. 😦

Work-Life Balance.

You can’t be successful in one day.

All the time people around you tell how to do it, Either you ignore  it or take to next level.

Better late than never.

Never Leave your Day job(even if it is cutting grass ).

Don’t try to be OVER-SMART.

Never consume any Addictive substance.

Learn to respect your personal space as well as others.

Learn to turn off your mind from consistence thinking of things.

Love your work—-Work is never Ending process, Don’t take so much pressure to complete it or start next one.

Have a group of friends outside work.

Nobody is slowing you down Except you.

Learn to say sorry, please, thanks, welcome.

Help others but respect your time and Energy.

Break the pattern of your life.

Be hungry, be foolish – Stop believing that.

Sikhism has different way of living life.(Either believe in that or Live with sorrows.)

If you want to earn more, Be-crazy, Get-exploited and create a big hole inside you, that is your choice as well. 🙂

Python For Yankees

Before going further reading about Class Development I would like you to tell
one important thing:

Most of the uses of inheritance can be simplifi ed or replaced with composition, and multiple
inheritance should be avoided at all costs.

I am sure after this you will be able to read lots and lots of code written in
python. 🙂 If you want to do it fast, Do it well. 🙂

Python Class Development Toolkit.

def __init__():

Don’t put anything in instance that you don’t need instance for.

def i_am_class_method(self):
return ‘takes self as argument’

If you need to shared data for whole class then put it on class level,
not on instance level.

class IAm(object):


def __init__(self):
print shared_data

def fun_2(self):
print shared_data

Iron_clad_rule:- in java or C++ is–>do not expose your attributes

Subclassing is just like inhertance. Data+methods:

Class NewOne(IAm):

def fun3(self):
return IAm.fun_2(self)

Multiple/Alternative-Constructors:(When you need to change the behavior
of class’s data by just calling an intensive function):

class Circle(object):

def __init__(self,radius):

def fun_2(self):
print self.radius

@classmethod   # Alternative constructive
def from_bbd(cls,new_radius):
“””construct a circle from bounding box diagnol”””

raduis = new_radius/2.0/math.sqrt(2.0)
return Circle(radius)

Make Alternate constructor to work for Subclass as well:

class Circle(object):

def __init__(self,radius):

def fun_2(self):
print self.radius

@classmethod   # Alternative constructive
def from_bbd(cls,new_radius):
“””construct a circle from bounding box diagnol”””

raduis = new_radius/2.0/math.sqrt(2.0)
return cls(radius)

Independent methods inside class: – Why we ever need those?
Think of situation where everything in your code breaks but you just need to tell
user something and something and all you need is 🙂 Static-Method.!!

class Circle(object):

def __init__(self,radius):

def fun_2(self):
print self.radius

def just_method(just_parameter):
return ‘I am independent’

Getters and Setters in Python:(Access Data,change data on the fly)
As in the other languages there are inbuilt methods to access data from
class as well as change values of method attributes in the class.

Python uses the property decorator to perform such operations:

class Person(object):
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name

def full_name(self):
return self.first_name + ‘ ‘ + self.last_name

def full_name(self, value):
first_name, last_name = value.split(‘ ‘)
self.first_name = first_name
self.last_name = last_name

def full_name(self):
del self.first_name
del self.last_name

Slots in Pyhton:
When you have lots and lots of things to perform or do in the way that
your class instance is consuming HUGE memory then you must use SLOTS.

Earlier approach:

class MyClass(object):
def __init__(self, name, identifier): = name
self.identifier = identifier

Approach after SLOTS:

class MyClass(object):
__slots__ = ['name', 'identifier']
def __init__(self, name, identifier): = name
self.identifier = identifier

Law of Wealthy LIfe

Wealthy life does not just mean to having lots of money in the bank but it is much more like creating various things in your society or running various engines those work in the manner that you are really able to make things happen in your life instantly, One thing you must remember or know carefully and that is If you really want to do it fast,  do it well. 🙂

Speed of implementation

Respect your time(Don’t waste on social media and stuff)

Go to bed early and get up early. Although I am writing this post so Late.:P 😦 😉

Alpha generation Quantitative

The average quantitative strategy may take from 10 weeks to seven months to develop, code, test and launch.[6] It is important to note that alpha generation platforms differ from low latency algorithmic trading systems. Alpha generation platforms focus solely on quantitative investment research rather than the rapid trading of investments. While some of these platforms do allow analysts to take their strategies to market, others focus solely on the research and development of these highly complex mathematical and statistical models.

%d bloggers like this: