-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·48 lines (33 loc) · 1017 Bytes
/
test.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
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
import threading, logging, time
import multiprocessing
from kafka import KafkaConsumer, KafkaProducer
class Producer(threading.Thread):
daemon = True
def run(self):
producer = KafkaProducer(bootstrap_servers='192.168.1.64:9092')
while True:
producer.send('test', b"Hello, world!")
time.sleep(1)
class Consumer(multiprocessing.Process):
daemon = True
def run(self):
consumer = KafkaConsumer(bootstrap_servers='192.168.1.64:9092',
auto_offset_reset='earliest')
consumer.subscribe(['test'])
for message in consumer:
print (message)
def main():
tasks = [
Producer(),
Consumer()
]
for t in tasks:
t.start()
time.sleep(10)
if __name__ == "__main__":
logging.basicConfig(
format='%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s',
level=logging.INFO
)
main()