python iterators and thread-safety

Posted by Igor on Stack Overflow See other posts from Stack Overflow or by Igor
Published on 2010-04-30T11:02:30Z Indexed on 2010/04/30 11:07 UTC
Read the original article Hit count: 290

I have a class which is being operated on by two functions. One function creates a list of widgets and writes it into the class:

def updateWidgets(self):
   widgets = self.generateWidgetList()
   self.widgets = widgets

the other function deals with the widgets in some way:

def workOnWidgets(self):
   for widget in self.widgets:
      self.workOnWidget(widget)

each of these functions runs in it's own thread. the question is, what happens if the updateWidgets() thread executes while the workOnWidgets() thread is running?

I am assuming that the iterator created as part of the for...in loop will keep some kind of reference to the old self.widgets object? So I will finish iterating over the old list... but I'd love to know for sure.

© Stack Overflow or respective owner

Related posts about multithreading

Related posts about thread-safety