Last active
May 25, 2018 19:07
-
-
Save norbertparti/0cb4659f1fa08e21618bfa1e777fdfa2 to your computer and use it in GitHub Desktop.
Multiprocessing parallel push to redis queue python
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
import time | |
from random import random | |
import json | |
from multiprocessing import Pool | |
import redis | |
r = redis.StrictRedis(host='localhost', port=6379, db=0) | |
queue_key = 'queue' | |
parallel = 30 | |
seq = 2500 | |
def push(val): | |
for i in range(seq): | |
_json = json.dumps({'key': random(), 'val': i * val}) | |
r.rpush('queue', _json) # loop here for multiple request sequentially | |
pool = Pool(10) | |
start = time.time() | |
args = (i for i in range(parallel)) | |
pool.map(push, args) | |
print(f'{parallel * seq} request time {time.time() - start}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment