-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstreaming_producer.py
34 lines (27 loc) · 1.08 KB
/
streaming_producer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import time
import json
import argparse
import streaming_generator
from kafka import SimpleProducer, KafkaClient
def timed_call(fn, calls_per_second, *args, **kwargs):
start = time.time()
fn(*args, **kwargs)
fn_time = time.time() - start
sleep_duration = max(0, (1.0 - calls_per_second * fn_time) / calls_per_second)
print sleep_duration
while True:
fn(*args, **kwargs)
time.sleep(sleep_duration)
def send_message(producer, topic):
message_raw = streaming_generator.gen_random_message()
producer.send_messages(topic, json.dumps({'user_id': message_raw[0],
'activity': message_raw[1]}))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--messages', default=1000)
parser.add_argument('-z', '--host', default="127.0.0.1:9092")
parser.add_argument('-t', '--topic', default='messages')
args = parser.parse_args()
kafka = KafkaClient(args.host)
producer = SimpleProducer(kafka)
timed_call(send_message, args.messages, producer, args.topic)