简体   繁体   English

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

[英]Execute multi threaded program in Python to run as single thread

I've been using threading class alot in my Python codes and have successfully make a couple routine to run as multi-threaded. 我一直在我的Python代码中使用线程类很多,并成功地使一些例程以多线程运行。 The problem is when something fails, it becomes a pain when it comes to debugging. 问题是当某些东西失败时,在调试方面会变得很痛苦。 This is what my code block would look like: 这就是我的代码块看起来像:

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()

The question is, How can I force the program to run as single threaded in order to ease debug. 问题是,如何强制程序作为单线程运行以便于调试。

There is a dummy_threading module in standard library that you can use instead of threading . 标准库中有一个dummy_threading模块,您可以使用它来代替threading It offers the same inteface but runs code sequentially ( start returns when the thread has finished). 它提供相同的接口,但顺序运行代码(当线程完成时start返回)。 Of course, your threads must not rely on each other to work in parallel. 当然,您的线程不能相互依赖并行工作。

import dummy_threading as threading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM