This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# send a msg from another app to RQ | |
from kombu import Connection | |
from tasks import add | |
from time import sleep | |
queue_name = 'tasks' | |
while True: | |
try: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Example using coroutines within Mutex | |
# | |
import asyncio | |
MUTEX = asyncio.Lock() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Example using coroutines within Threads and blocking task | |
# | |
import time | |
import asyncio | |
from concurrent.futures import ThreadPoolExecutor | |
def blocking_task(num): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Example using coroutines | |
# | |
import time | |
import asyncio | |
async def task(num): | |
# Wait 1s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Multiprocessing using Pool | |
# | |
import time | |
from multiprocessing import Pool, cpu_count | |
def task(arg): | |
return arg ** 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# MultiProcessing using Mutex example | |
# | |
import time | |
from multiprocessing import Process, Lock, Array, Value | |
def task(index, result): | |
result[index.value] = index.value ** 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Threading with/without mutex | |
# | |
import time | |
import threading | |
MUTEX = threading.Lock() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A: | |
dependencies = ["x", "y"] | |
@classmethod | |
def get_dep(cls): | |
dep = [] | |
for k in cls.__mro__[:-1]: | |
dep += k.__dict__.get('dependencies', []) | |
return dep |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Project Euler: Problem 34 | |
# 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. | |
# | |
# Find the sum of all numbers which are equal to the sum of the factorial of their digits. | |
# | |
# Note: as 1! = 1 and 2! = 2 are not sums they are not included. | |
################################ | |
# Solution: 40730 | |
# Best time: 9.244094610214233 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
To install virtualenv via pip | |
$ pip3 install virtualenv | |
Note that virtualenv installs to the python3 directory. For me it's: | |
$ /usr/local/share/python3/virtualenv | |
Create a virtualenvs directory to store all virtual environments | |
$ mkdir somewhere/virtualenvs | |
Make a new virtual environment with no packages |
NewerOlder