Python Frequently Asked Questions, Release 3.9.5 51 4.3. Threads import threading , time def thread_task(name, n): for i in range(n): print(name, i) for i in range(10 ): T =threading.Thread(target=thread_task, args =(str(i), i)) T.start() The “global main logic” of your program may be as simple as if __name__ =="__main__ ": main_logic() at the bottom of the main module of your program. Once your program is organized as a tractable collection of function and class behaviours, you should write test functions that exercise the behaviours. A test suite that automates a sequence of tests can be associated with each module. This sounds like a lot of work, but since Python is so terse and flexible it’s surprisingly easy. You can make coding much more pleasant and fun by writing your test functions in parallel with the “production code”, since this makes it easy to find bugs and even design flaws earlier. “Support modules” that are not intended to be the main module of a program may include a self-test of the module. if __name__ =="__main__ ": self_test() Even programs that interact with complex external interfaces may be tested when the external interfaces are unavail- able by using “fake” interfaces implemented in Python. The$_{pydoc}$module can create HTML from the doc strings in your Python source code. An alternative for creating API documentation purely from docstrings is epydoc. Sphinx can also include docstring content. For Unix variants there are several solutions. It’s straightforward to do this using curses, but curses is a fairly large module to learn. Be sure to use the$_{threading}$ module and not the$_{_thread}$module. The$_{threading}$ module builds convenient abstractions on top of the low-level primitives provided by the$_{_thread}$module. Aahz has a set of slides from his threading tutorial that are helpful; see http://www.pythoncraft.com/OSCON2001/. As soon as the main thread exits, all threads are killed. Your main thread is running too quickly, giving the threads no time to do any work. A simple fix is to add a sleep to the end of the program that’s long enough for all the threads to finish: (continues on next page) 4.3.2 None of my threads seem to run: why? 4.3.1 How do I program using threads? 4.3 Threads 4.2.3 How do I get a single keypress at a time? 4.2.2 How do I create documentation from doc strings?