Infinite loops and synchronization

pdpi pdpinheiro at gmail.com
Mon Jul 13 12:12:03 CEST 2009


On Jul 13, 6:06 am, Vincent Gulinao <vincent.guli... at gmail.com> wrote:
> lst = list()
>
> (lst populated by async twisted deferred callbacks)
>
> while True:
>         if len(lst) == SOME_NUMBER:
>                 return lst
>
> Q1: is this a common OK practice? I'm worried infinite loops hogs memory.
> Q2: operating on list from threads (mostly appends) must be safe,
> right (synchronization)?

Q1: I'll answer your question with another. What's so fundamentally
different between your infinite loop and this one:

while len(lst) != SOME_NUMBER:
  pass
return lst

which is not an "infinite loop"[1]. Why would yours be any worse in
terms of memory than mine? Are you allocating anything that would hog
memory? Of course, like Piet said, it *will* hog your CPU, so you want
a time.sleep(.1) in there, at the least. Of course, the question is:
why aren't you using a semaphore to let you know you can proceed, and
make the provider increment the semaphore?

[1] -- well, it can be, if len(lst) == SOME_NUMBER never comes about,
and I'd hazard a guess that that's pretty much where your fear of
memory hogging comes from: it's easy to allocate stuff and not
deallocate it within a cycle, only to find the bounds on that cycle
going awry.



More information about the Python-list mailing list