File tree Expand file tree Collapse file tree 1 file changed +32
-8
lines changed Expand file tree Collapse file tree 1 file changed +32
-8
lines changed Original file line number Diff line number Diff line change 6
6
7
7
8
8
class Producer (threading .Thread ):
9
- daemon = True
9
+ def __init__ (self ):
10
+ threading .Thread .__init__ (self )
11
+ self .stop_event = threading .Event ()
12
+
13
+ def stop (self ):
14
+ self .stop_event .set ()
10
15
11
16
def run (self ):
12
17
producer = KafkaProducer (bootstrap_servers = 'localhost:9092' )
13
18
14
- while True :
19
+ while not self . stop_event . is_set () :
15
20
producer .send ('my-topic' , b"test" )
16
21
producer .send ('my-topic' , b"\xc2 Hola, mundo!" )
17
22
time .sleep (1 )
18
23
24
+ producer .close ()
19
25
20
26
class Consumer (multiprocessing .Process ):
21
- daemon = True
22
-
27
+ def __init__ (self ):
28
+ multiprocessing .Process .__init__ (self )
29
+ self .stop_event = multiprocessing .Event ()
30
+
31
+ def stop (self ):
32
+ self .stop_event .set ()
33
+
23
34
def run (self ):
24
35
consumer = KafkaConsumer (bootstrap_servers = 'localhost:9092' ,
25
- auto_offset_reset = 'earliest' )
36
+ auto_offset_reset = 'earliest' ,
37
+ consumer_timeout_ms = 1000 )
26
38
consumer .subscribe (['my-topic' ])
27
39
28
- for message in consumer :
29
- print (message )
30
-
40
+ while not self .stop_event .is_set ():
41
+ for message in consumer :
42
+ print (message )
43
+ if self .stop_event .is_set ():
44
+ break
31
45
46
+ consumer .close ()
47
+
48
+
32
49
def main ():
33
50
tasks = [
34
51
Producer (),
@@ -39,7 +56,14 @@ def main():
39
56
t .start ()
40
57
41
58
time .sleep (10 )
59
+
60
+ for task in tasks :
61
+ task .stop ()
42
62
63
+ for task in tasks :
64
+ task .join ()
65
+
66
+
43
67
if __name__ == "__main__" :
44
68
logging .basicConfig (
45
69
format = '%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s' ,
You can’t perform that action at this time.
0 commit comments