在Python中执行多线程程序以单线程运行

时间:2013-10-28 07:13:33

标签: python multithreading python-multithreading single-threaded

我已经在我的Python代码中使用了很多线程类,并成功地使用了几个例程来运行多线程。问题是当某些东西失败时,在调试方面会变得很痛苦。这就是我的代码块的样子:

threads = []
for argument in get_file_method():
    thread = threading.Thread(self._routine, argument)
    thread.start()
    threads.append(thread)

# Wait for all threads to complete
for thread in threads:
    filename = thread.join()

问题是,如何强制程序以单线程运行以便于调试。

1 个答案:

答案 0 :(得分:2)

您可以使用标准库中的dummy_threading模块代替threading。它提供相同的接口,但顺序运行代码(线程完成后返回start)。当然,你的线程不能相互依赖并行工作。

import dummy_threading as threading