如何在Python中连接线程

如何在Python中连接线程,python,multithreading,Python,Multithreading,我的主线程启动多个为http和grpc端点服务的线程 主线 s1 = Server1() s2 = Server2() Server1和Server2是类似于的类 class Server1(threading.Thread): def __init__(self, service): super().__init__() self.grpcServer(futures.ThreadPoolExecutor(max_workers=10)) self.grpcSer

我的主线程启动多个为http和grpc端点服务的线程

主线

s1 = Server1()
s2 = Server2()
Server1和Server2是类似于的类

class Server1(threading.Thread):
  def __init__(self, service):
    super().__init__()
    self.grpcServer(futures.ThreadPoolExecutor(max_workers=10))
    self.grpcServer.add_insecure_port('[::]:8000')
    myservice_pb2_grpc.add_MyServiceServicer_to_server(service, self.grpcServer)

  def run(self):
    self.grpcserver.start()
    self.grpcServer.wait_for_termination()
现在我想修改一下,如果有任何并行线程退出(服务器中的任何类型的故障),主线程和应用程序也会退出。(应用程序退出会导致其docker容器退出,Kubernetes会检测到这一点,从而重新启动pod,这比让容器运行的应用程序有一个死端点要好得多,直到有人尝试使用该端点,才知道该端点)

我想用

将以下内容添加到主线程:

s1.join()
s2.join()

但这不起作用,我在
s1.join()
中得到了
AttributeError:'NoneType'对象没有属性“join”
。我不太懂Python,所以我确信这是一个愚蠢的误解,我只是不明白。

s1
。检查分配给他们的是什么。我不确定这是什么意思。s1在
s1=Server1()
处分配给Server1()。我假设这意味着s1属于从threading.Thread派生的类,因此可以在s1上调用join方法。我猜我错了,但为什么?口译员说它是
,你认为它不是。不幸的是,解释器就是运行代码的解释器。向我们展示所有相关代码!如果
s1
确实被分配为
Server1()
,您将得到另一个错误,因为您没有将
服务
参数传递给
Server1()
。很明显,现在的问题中缺少了一些东西。我猜你不知何故混合了一个同名的全局变量(可以存在于函数的外部和内部)和局部变量(仅存在于函数内部)。您的代码示例太小,无法判断我是否正确。需要更多的背景。