Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法启动多个服务器线程_Python_Multithreading - Fatal编程技术网

Python 无法启动多个服务器线程

Python 无法启动多个服务器线程,python,multithreading,Python,Multithreading,我正在尝试用python创建一个程序,在这个程序中我必须同时运行两个类。所以我要做的就是为类的每个实例创建一个线程。编程对我来说是相当新的,所以这就是我们所说的“用户错误”;) 这是我的密码: import threading import sys servers = [ ["Server 01", "192.168.0.1", 12345, "password"], ["Server 02", "192.168.0.1", 12346, "password"], ] def

我正在尝试用python创建一个程序,在这个程序中我必须同时运行两个类。所以我要做的就是为类的每个实例创建一个线程。编程对我来说是相当新的,所以这就是我们所说的“用户错误”;)

这是我的密码:

import threading
import sys

servers = [
    ["Server 01", "192.168.0.1", 12345, "password"],
    ["Server 02", "192.168.0.1", 12346, "password"],
]

def main():
    for row in servers:
        serverName = row[0]
        serverAddress = row[1]
        rconPort = row[2]
        rconPass = row[3]
        th = threading.Thread(target = rcon.RconProtocol(serverAddress, rconPort, rconPass, serverName))
        th.start()

if __name__ == '__main__':
    main()
该代码对于
servers
中的第一台服务器非常有效。但是它在
th.start()之后停止。所以无论我在
th.start()
之后做什么,它都不会运行
RconProtocol
除了监听套接字之外,没有做任何特殊的事情。这就是我认为问题所在。我在
RconProtocol
类中有一个无限循环(
while True

我尝试了一个类似的功能(我也做了另一个名为
two
):

然后把它指定为我线程的目标,我也遇到了同样的问题。它在没有while循环的情况下工作(只需打印“一”)

所以我有点好奇为什么会这样,我做错了什么。我通常认为自己是一个谷歌高手,但我找不到任何类似的例子。我的头脑不喜欢那些你必须使用时间的例子,或者随机的例子。我想我必须看到更多的实际例子

顺便问一下,在这样的类中放置while循环是否“错误”?它应该以某种方式在外面吗?我应该在我的
RconProtocol
类中创建线程吗

希望这是足够的信息

提前谢谢


/Daniel

您正在主色拉中调用rcon.RconProtocol。回复以下行

th = threading.Thread(target=rcon.RconProtocol(serverAddress, rconPort, rconPass, serverName))


此外,在线程中使用while循环作为高级控件是个坏主意。Python线程非常适合于阻塞这样的情况:您将大部分时间花在等待某件事情上,然后做出反应。在您的情况下,您应该使用
线程.Queue
将命令发送到线程并转发到服务器。这样,线程将被阻止等待队列或向服务器发送消息

th = threading.Thread(target=rcon.RconProtocol(serverAddress, rconPort, rconPass, serverName))
th = threading.Thread(target=rcon.RconProtocol, args=(serverAddress, rconPort, rconPass, serverName))