Last active
March 29, 2019 16:52
-
-
Save lega911/3f60004bd0e4caab4e37d753569e4476 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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) |
This file contains hidden or 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
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) |
This file contains hidden or 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
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) |
This file contains hidden or 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
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