[Tutor] What's the difference between Queue(in module Queue) and list(build_in class)?

Steven D'Aprano steve at pearwood.info
Mon Feb 13 17:05:22 CET 2012


daedae11 wrote:
> What's the difference between Queue(in module Queue) and list(build_in class)? 
> I think that when I pass a same queue object to two different threads, the change in each thread will affect each other as same as Queue. So why don't we use list or other variable object?


Have you read the Fine Manual?

Queue has a much more powerful and richer interface designed for use as a 
queue, while list is a general purpose sequence that can only be used for the 
most primitive queue-like data structures.

For example, Queue can be set to have a maximum size, while list will always 
grow until you run out of memory.

Queue.get() by default will wait as long as necessary for a value to be 
available, while list.pop() will fail immediately if the list is empty.

Queue is also carefully written to be thread-safe, unlike most other data 
structures written in pure Python. (I expect that the built-in list is also 
thread-safe, but I don't know for sure.)

By thread-safe I mean that two threads cannot accidentally delete or write to 
the same item at the same time. Thread safety is very hard to get right, so 
you should always use Queue with threads.



-- 
Steven



More information about the Tutor mailing list