Skip to content

Instantly share code, notes, and snippets.

@lega911
Last active March 29, 2019 16:52
Show Gist options
  • Save lega911/3f60004bd0e4caab4e37d753569e4476 to your computer and use it in GitHub Desktop.
Save lega911/3f60004bd0e4caab4e37d753569e4476 to your computer and use it in GitHub Desktop.
import redis
import time
import json
r = redis.Redis(host='localhost', port=6379, db=0)
for i in range(100):
print(i)
r.rpush('tasks', json.dumps({'task': i}))
time.sleep(0.5)
import redis
import os
import json
import time
import math
NUMBER_WORKERS = 8 # 1, 2, 4, 8
for _ in range(int(math.log2(NUMBER_WORKERS))):
print('fork')
os.fork()
r = redis.Redis(host='localhost', port=6379, db=0)
while True:
task = json.loads(r.blpop('tasks')[1])
print('worker', os.getpid(), task)
time.sleep(2)
import redis
import json
import time
r = redis.Redis(host='localhost', port=6379, db=0)
while True:
task = json.loads(r.blpop('tasks')[1])
print(task)
time.sleep(2)
import redis
import json
import time
import threading
NUMBER_WORKERS = 8 # 1, 2, 4, 8
def worker(n):
r = redis.Redis(host='localhost', port=6379, db=0)
while True:
task = json.loads(r.blpop('tasks')[1])
print('worker', n, task)
time.sleep(2)
for i in range(NUMBER_WORKERS):
p = threading.Thread(target=worker, args=(i, ))
p.start()
p.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment