Python多进程队列get()块

时间:2017-03-13 14:40:43

标签: python linux sockets tcp multiprocessing

我写了一个小代理实用程序(tcp port-forwarder),现在尝试测试它。 所以我的测试:

def test_forwarding(self):
    route = self.config.routes[0]
    q = multiprocessing.JoinableQueue()

    proc_listen = multiprocessing.Process(
        target=lambda q: q.put(str(
            subprocess.Popen(["nc", "-l", str(route.sink[1])], stdout=subprocess.PIPE, shell=False).communicate()[0])),
        args=(q,))
    proc_listen.start()

    proc_write = multiprocessing.Process(
        target=lambda: subprocess.Popen("nc %s %s < ~/Workshop/port-forwarder/tests/test_data.txt" % route.source,
                                        stdout=subprocess.PIPE, shell=True))
    proc_write.start()

    proc_write.join(3)
    proc_write.terminate()

    proc_listen.join(3)
    proc_listen.terminate()
    q.join()
    self.assertEqual(open("test_data.txt", 'r').readline(), q.get())

但测试卡在get()调用断言。完全搞砸了,试过加入/终止等所有东西。任何帮助将非常感激。

以下是github repo,来源:enter link description here

1 个答案:

答案 0 :(得分:3)

如果你被get阻止,你可以

  1. 使用get_nowait()

  2. 或使用empty()检查是否有一些元素。如果不是空的则获取,否则跳过