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

在Python中执行多线程程序以作为单线程运行,python,multithreading,python-multithreading,single-threaded,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

我在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()
问题是,我如何强制程序作为单线程运行以简化调试。

标准库中有一个模块,您可以使用它来代替
线程。它提供相同的接口,但按顺序运行代码(
start
在线程完成时返回)。当然,线程之间不能相互依赖以并行工作

import dummy_threading as threading

那没有道理。程序什么时候运行,以什么顺序运行?谢谢Janne Karila,这很有帮助。但是,有没有一种方法可以显式指定允许程序生成多少线程?@superface我想说,您的程序应该控制它生成多少线程。也许这对你有用: