You might want to look at python queue implementation.