python线程方法卡住 [英] python threading method stuck

查看:35
本文介绍了python线程方法卡住的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 MyClass ,该类在初始化时创建7个线程.一个线程是TCPServer,其他六个线程是 MyClass 的对象,TCPServer用于处理请求.

I have a class MyClass which creates 7 threads when it is initialized. One thread is a TCPServer, and the other six are objects of MyClass which the TCPServer uses to process requests.

我的意图是创建一种可以在 MyClass 后台运行并维护6个线程的方法.这6个线程对应于2个分布式对象 obj1 obj2 ,这些对象是通过称为PySyncObj的Raft实现复制的.每个对象有三个线程.

My intention is to create method which can run in the background of MyClass and maintain the 6 threads. The 6 threads correspond to 2 distributed objects obj1 and obj2 replicated with an implementation of Raft called PySyncObj. There are three threads per object.

每个对象群集 obj_1_cluster obj_2_cluster 都有一个领导者,服务器必须跟踪该领导者,因为对对象的所有更改都必须仅发送给该领导者.

Each cluster of objects obj_1_cluster and obj_2_cluster has a leader, which the server must keep track of, because all changes to the object must be sent only to the leader.

class MyClass(object):

def __init__(self):

    self.server = TCPServer()

    self.obj_1_cluster = self.init_cluster(Class_1['bindings'], Class_1)
    self.server.obj1 = self.get_leader(obj_1_cluster)

    self.obj_2_cluster = self.init_cluster(obj_2['bindings'], Class_2)
    self.server.obj2 = self.get_leader(obj_2_cluster)


    # Create Daemon process for keeping leader current.
    self.obj1_leader_daemon = Thread(target = self.__obj1_leader_daemon())
    self.obj1_leader_daemon.setDaemon(True)
    self.obj1_leader_daemon.start()

    self.obj2_leader_deamon = Thread(target = self.__obj2_leader_daemon())
    self.obj2_leader_deamon.setDaemon(True)
    self.obj2_leader_deamon.start()

def __obj1_leader_daemon(self):
    while True:
        print("Running obj1 daemon")
        if self.server.obj1._isLeader():
            pass
        else:
            self.server.obj1 = self.get_leader(self.obj1)

def __obj2_leader_daemon(self):
    while True:
        print("running obj2 daemon")
        if self.server.obj2._isLeader():
            pass
        else:
            self.server.obj2 = self.get_leader(self.obj2)

运行此代码时,我看到的唯一输出是...

When I run this code, the only output I see is...

Running obj1 daemon
Running obj1 daemon
Running obj1 daemon
...

直到我使用ctrl-C终止该进程.

until I kill the process with ctrl-C.

是否可以更改此方法,以便这两个进程可以使用自己的线程运行,以在需要更改对象的情况下忙于检查对象的状态?我已经阅读了很多有关线程的知识,但我不明白为什么它目前无法按照我认为的方式工作.

Is there a way to change this so that these two processes can run with their own thread- busily checking the state of the objects in case they need to change them? I've read a lot about threading and I don't see why this isn't currently working the way I think it should.

环境:我正在MacOS 10.13.3上运行python 2.7.14.

Environment: I'm running python 2.7.14 on MacOS 10.13.3.

推荐答案

__ init __ 被困在

self.obj1_leader_daemon = Thread(target = self.__obj1_leader_daemon())

线程目标应该是要运行的函数的名称,但是您已经调用了该函数. self .__ obj1_leader_daemon()是一个无限循环,因此它永远不会返回,没有为 target 分配任何内容,也不会创建线程.您的主线程挂起并运行守护程序代码.

The thread target should be the name of the function to run, but you've called the function instead. self.__obj1_leader_daemon() is an infinite loop so it never returns, nothing is assigned to target and no thread is ever created. Your main thread hangs running the daemon code.

只需移开牛仔腿

self.obj1_leader_daemon = Thread(target = self.__obj1_leader_daemon)

...并对其他线程执行相同操作.

... and do the same for the other thread.

这篇关于python线程方法卡住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆