Python version of "semaphores" example code