使用threading.Thread和Queue时出现分段错误

时间:2014-12-28 02:50:18

标签: python-2.7 segmentation-fault queue python-multithreading

我试图弄清楚为什么我会继续从以下方法中获取段错误。

import threading, Queue

Class A:

    @staticmethod
    def f_A(x, y):

        """
        There is a call to numpy.integrate.quad here
        """
        ...
        return z


Class B:

    @staticmethod
    def worker(f, q, x, y):

        ...
        q.put(f(x, y))
        return

    @staticmethod
    def f_B(x1, y1, x2, y2):

        f = A.f_A
        q = Queue()

        thread1 = threading.Thread(target=B.worker, args=(f, q, x1, y1))
        thread2 = threading.Thread(target=B.worker, args=(f, q, x2, y2))

        thread1.start()
        thread2.start()

        thread1.join()
        thread2.join()

        result1 = q.get()
        result2 = q.get()

        return result1, result2

In [1]: B.f_B(x1, y1, x2, y2)
Segmentation fault (core dumped)

xy列表。如果我运行单个线程,一切正常。有什么想法为segfaulting?是因为每个线程都试图访问同一个函数A.f_A()吗?

奇怪的是,如果我使用multiprocessing.Process它也有效。为什么它不能与线程一起使用?

0 个答案:

没有答案