在单独的线程中启动服务器实例未将服务器对象传递给方法

问题描述 投票:0回答:1

[我试图在一个单独的线程中启动Stanford NLP服务器,并将服务器实例作为参数传递给类run_nlp中定义的方法Grid,该类是我的python应用程序的布局类。

from readUseCase import parser
...
...
class Grid(Widget):
    text_from_file = ObjectProperty(None)
    client = ObjectProperty(None)

    def start_server(self):
        with CoreNLPClient(properties='./server.props') as self.client:
            pass

    def run_nlp(self):
        parser(self.client, self.text_from_file.text)

class Main(App):
    def build(self):
        thread1 = threading.Thread(target=Grid().start_server)
        thread1.start()
        return Grid()

if __name__ == '__main__':
    Main().run()

所以发生的是,我启动了应用程序,thread启动了,控制台显示服务器正在运行,我的应用程序也正在运行并等待用户输入。但是,当我为NLP输入一些文本并按下按钮以启动方法run_nlp()时,函数parser()需要2个输入参数,第一个是服务器对象,第二个是输入文本。

函数解析器:

def parser(client, text):

    # submit the request to the server
    ann = client.annotate(text)      //AttributeError("NoneType" object has no attribute "annotate")

我在self.text_from_file.text参数中获得了正确的值,但是对于NoneType参数却获得了self.client,这就是我不知道为什么的原因。我试图将其分配给其他变量,或者我尝试分配给return self.client,但这些都不起作用。

我叫start_server对吗?或问题可能出在哪里?

python multithreading stanford-nlp
1个回答
0
投票

也许是因为您在Grid方法中创建了两个build类型的对象。一个用于启动线程,但另一个用于创建并返回。我认为您应该将代码更改为以下内容:

class Main(App):
    def build(self):
        grid = Grid()
        thread1 = threading.Thread(target=grid.start_server)
        thread1.start()
        return grid

另一个问题可能是,客户端没有设置。我不知道这个库,但是请确保您正确设置了所有内容。

© www.soinside.com 2019 - 2024. All rights reserved.