1

I've been using threading class alot in my Python codes and have successfully make a couple routine to run as multi-threaded. 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.

superface
  • 73
  • 6

1 Answers1

2

There is a dummy_threading module in standard library that you can use instead of threading. It offers the same inteface but runs code sequentially (start returns when the thread has finished). Of course, your threads must not rely on each other to work in parallel.

import dummy_threading as threading
Janne Karila
  • 24,266
  • 6
  • 53
  • 94
  • Thanks Janne Karila, this helps. But, is there a method i can explicitly specify how many threads the program is allowed to spawn? – superface Oct 29 '13 at 08:53
  • @superface I would say that your program should control how many threads it spawns. Maybe this is useful to you: [Python thread pool similar to the multiprocessing Pool?](http://stackoverflow.com/q/3033952/222914) – Janne Karila Oct 29 '13 at 11:01
  • 1
    The `dummy_threading` module was removed in Python 3.9. `ModuleNotFoundError: No module named 'dummy_threading'` – gerrit Jun 25 '21 at 12:31