3

I've used multi-threading a lot, while appending to the same list from different threads. Everything worked fine.

However, I'm getting a problem with list appending when the threads are like 70 threads or more. Appending in the last thread gets stuck for like 5 mins (the processor is not occubied at this time, maybe 10 %. So, it's not a hardware problem I would say). Then appending occurs successfully.

At this link, it says that list appending is thread-safe.

My question is: Can list appending ever become not thread safe?

Don't ask for a code or so. I just need a simple yes or no to my question. And if yes, kindly provide suggestions to fix that.

James Z
  • 12,209
  • 10
  • 24
  • 44
AhmedWas
  • 1,205
  • 3
  • 23
  • 38
  • 1
    The thread safety is effectively [achieved by Python through the GIL](https://stackoverflow.com/questions/6319207/are-lists-thread-safe) and with 70 threads it is possible for a single thread to be locked out and not be able to do anything for quite some time as only one will be executing at a time. – metatoaster Dec 18 '18 at 13:34

1 Answers1

1

list appending in python is thread safe.

For more details: what kinds of global value mutation are thread safe

You last thread gets stuck maybe due to other reason, for example: Memory allocating..

My first step to fix the stuck is use strace to trace the syscall.

And you can use gdb to print all threads' call stack too. Here is a wiki page: https://wiki.python.org/moin/DebuggingWithGdb

Xiwei Wang
  • 346
  • 4
  • 17