PerThreadList.py :  » Web-Frameworks » Webware » Webware-1.0.2 » MiddleKit » Run » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Webware 
Webware » Webware 1.0.2 » MiddleKit » Run » PerThreadList.py
#from UserListimport UserList
#from UserDict import UserDict

import thread


class PerThreadList:
  """Per-thread list.

  PerThreadList behaves like a normal list, but changes to it are kept
  track of on a per-thread basis.  So if thread A appends an item to
  the list, only thread A sees that item.  There are a few non-standard
  methods (clear, isEmpty), too.

  This is implementated by keeping a dictionary of lists; one for each
  thread. The implementation is not a complete list wrapper; only some
  methods are implemented. If more methods are needed, see UserList
  (in the standard Python lib) for inspiration.

  """

  def __init__(self):
    self.data = {}

  def append(self, item, gettid=thread.get_ident):
    threadid = gettid()
    try:
      self.data[threadid].append(item)
    except KeyError:
      self.data[threadid] = [item]

  def extend(self, list, gettid=thread.get_ident):
    threadid = gettid()
    try:
      self.data[threadid].extend(list)
    except KeyError:
      self.data[threadid] = list

  def clear(self, allThreads=0, gettid=thread.get_ident):
    """Erases the list, either for the current thread or for all threads.

    We need this method, because it obviously won't work for user code
    to do: list = [].

    """
    if allThreads:
      self.data = {}
    else:
      threadid = gettid()
      try:
        self.data[threadid] = []
      except Exception:
        pass

  def items(self, allThreads=0, gettid=thread.get_ident):
    if allThreads:
      items = []
      for l in self.data.values():
        items.extend(l)
      return items
    else:
      threadid = gettid()
      try:
        return self.data[threadid]
      except KeyError:
        return []

  def isEmpty(self, gettid=thread.get_ident):
    """Test if the list is empty for all threads."""
    for l in self.data.values():
      if l:
        return 0
    return 1

  def __len__(self, gettid=thread.get_ident):
    threadid = gettid()
    try:
      return len(self.data[threadid])
    except KeyError:
      return 0

  def __getitem__(self,  i, gettid=thread.get_ident):
    threadid = gettid()
    if self.data.has_key(threadid):
      return self.data[threadid][i]
    else:
      return [][i]


class NonThreadedList:
  """Non-threaded list.

  NonThreadedList behaves like a normal list.  Its only purpose is
  to provide a compatible interface to PerThreadList, so that they
  can be used interchangeably.

  """

  def __init__(self):
    self.data = []

  def append(self, item):
    self.data.append(item)

  def extend(self, list):
    self.data.extend(list)

  def items(self, allThreads=0):
    return self.data

  def clear(self, allThreads=0):
    """Erases the list.

    We need this method, because it obviously won't work for user code
    to do: list = [].

    """
    self.data = []

  def __len__(self):
    return len(self.data)

  def __getitem__(self, i):
    return self.data[i]

  def isEmpty(self):
    """Test if the list is empty for all threads."""
    return len(self.data) == 0


if __name__ == '__main__':
  # just a few tests used in development
  def addItems():
    global l
    l.append(1)
    l.append(2)
  global l
  l = PerThreadList()
  for i in l:
    print i
  l.append(1)
  assert len(l) == 1
  l.append(2)
  l.append(3)
  assert len(l) == 3
  for i in l:
    print i
  from threading import Thread
  t = Thread(target=addItems)
  t.start()
  t.join()
  assert len(l) == 3
  assert len(l.items()) == 3
  assert len(l.items(allThreads=1)) == 5
  l.clear()
  assert len(l.items(allThreads=1)) == 2
  l.clear(allThreads=1)
  assert len(l.items(allThreads=1)) == 0
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.