@@ -60,6 +60,19 @@ def create_pipe_event(
6060 return _pipe_event
6161
6262
63+ def run_direction (from_bot : Dict [str , Any ], to_bot : Dict [str , Any ], stream_wide : bool ) -> None :
64+ """
65+ Child-process entrypoint. Each child constructs its own clients so no Session
66+ objects are ever inherited or pickled.
67+ """
68+ from_client = zulip .Client (
69+ email = from_bot ["email" ], api_key = from_bot ["api_key" ], site = from_bot ["site" ]
70+ )
71+ to_client = zulip .Client (email = to_bot ["email" ], api_key = to_bot ["api_key" ], site = to_bot ["site" ])
72+ pipe_event = create_pipe_event (to_client , from_bot , to_bot , stream_wide )
73+ from_client .call_on_each_event (pipe_event , ["message" ])
74+
75+
6376if __name__ == "__main__" :
6477 usage = """run-interrealm-bridge [--stream]
6578
@@ -78,16 +91,12 @@ if __name__ == "__main__":
7891 args = parser .parse_args ()
7992
8093 options = interrealm_bridge_config .config
81-
8294 bot1 = options ["bot_1" ]
8395 bot2 = options ["bot_2" ]
84- client1 = zulip .Client (email = bot1 ["email" ], api_key = bot1 ["api_key" ], site = bot1 ["site" ])
85- client2 = zulip .Client (email = bot2 ["email" ], api_key = bot2 ["api_key" ], site = bot2 ["site" ])
86- # A bidirectional tunnel
87- pipe_event1 = create_pipe_event (client2 , bot1 , bot2 , args .stream )
88- p1 = mp .Process (target = client1 .call_on_each_event , args = (pipe_event1 , ["message" ]))
89- pipe_event2 = create_pipe_event (client1 , bot2 , bot1 , args .stream )
90- p2 = mp .Process (target = client2 .call_on_each_event , args = (pipe_event2 , ["message" ]))
96+
97+ ctx = mp .get_context ("spawn" )
98+ p1 = ctx .Process (target = run_direction , args = (bot1 , bot2 , args .stream ))
99+ p2 = ctx .Process (target = run_direction , args = (bot2 , bot1 , args .stream ))
91100 p1 .start ()
92101 p2 .start ()
93102 print ("Listening..." )
0 commit comments