multithreading之在 Python 中使用多线程运行 Iperf 服务器和客户端会导致段错误

zhengyun_ustc 阅读:125 2025-05-04 20:05:19 评论:0

一个主类调用另外两个类(IperfServer 和 IperfClient),我正在尝试使用多线程运行它们。我正在为 iperf3 使用 python 包装器类。这两个类都已启动,但在运行 Iperf 时,出现段错误。

代码片段:

class IperfServer(threading.Thread): 
    def __init__(self): 
        threading.Thread.__init__(self) 
 
    def run(self): 
        print("1") 
        server = iperf3.Server() 
        print("2") 
        server.port = 5201 
        response = server.run() 
 
class IperfClient(threading.Thread): 
    def __init__(self): 
        threading.Thread.__init__(self) 
 
    def run(self): 
        print("3") 
        connection = http.client.HTTPSConnection("abc.efg") 
        print("4") 
        connection.request(method="GET", url="/hij/") 
        response = connectn.getresponse() 
        connectn.close() 
 
        print("5") 
        client = iperf3.Client() 
        client.run() 
 
class IperfAgent(object): 
    thread1 = IperfClient() 
    thread2 = IperfServer() 
 
    thread1.start() 
    thread2.start() 

输出:

3

1

段错误

我是 Python 和多线程的新手。有人可以帮我找出我犯的错误吗?

请您参考如下方法:

尝试在子进程(参见 multiprocessing.Process)而不是线程中运行它。

看来 iperf_defaults 需要在主线程上运行。


标签:多线程
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号