What does dummy_threading do?

multithreading, python, python-2.7, python-multithreading

Solution

The module is meant to be used when `thread` and `threading` are not available on your platform.

The functions you pass to it are called synchronously, and you called `.start()` immediately. The first function acquires the semphore, prints, then the second function is called and blocks. It is run synchronously and never returns.

From the `dummy_thread` documentation:

Be careful to not use this module where deadlock might occur from a thread being created that blocks waiting for another thread to be created. This often occurs with blocking I/O.

and from an EFF-bot post (otherwise short and missing a working link):

Helpers to make it easier to write code that use threads where supported, but still runs on Python versions without thread support. The dummy modules simply run the threads sequentially.

Note the making it easier part; without actual threads, you cannot expect code run under `dummy_threading` to not deadlock in the manner that it does in your example.

Problem

When would I use `dummy_threading`? I had thought it might replace system-level threads with mock threads in case system-level threads weren't available for Python. But when I run this: ``` import dummy_threading as threading semaphore = threading.Semaphore() def f(i): semaphore.acquire() print i for i in xrange(10): threading.Thread(target=f, args=(i,)).start() for _ in xrange(10): semaphore.release() ``` I get `0`, and the program doesn't terminate. Not only that, but Python inexplicably continues to chew up my computer's memory until it has nothing left. When I run this: ``` import threading semaphore = threading.Semaphore() def f(i): semaphore.acquire() print i for i in xrange(10): threading.Thread(target=f, args=(i,)).start() for _ in xrange(10): semaphore.release() ``` I get `0 1 3 5 7 9 2 4 6 8` as expected. I must misunderstand `dummy_threading`. When would I use it? FYI, I tried this on Windows 7 and Fedora 18 for comparison and got the same results. EDIT: The following, however, gives `0 1 2 3 4 5 6 7 8 9`: ``` import dummy_threading as threading event = threading.Event() def f(i): event.wait() print i for i in xrange(10): threading.Thread(target=f, args=(i,)).start() event.set() ``` The big question is: what does `dummy_threading` do, or alternatively when would it give the same behavior as `threading`?

Original source