By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,427 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,427 IT Pros & Developers. It's quick & easy.

Are arguments to python __init__ method passed as pointers? In a threading.Thread?

P: 1
Hi,

I'm new to python, and having problems instantiating classes. My main method has a couple of lines like this:
Expand|Select|Wrap|Line Numbers
  1.     debug = False
  2.     stats_d = {"cpu":[],"foo":[],"bar":[],"baz":[]}
  3.     cpu.CpuMonitor(debug,stats_d["cpu"]).start()
  4.     db.UpdateDatabase(debug,stats_d).start()
  5.  
And CpuMonitor looks like this:
Expand|Select|Wrap|Line Numbers
  1. class CpuMonitor (threading.Thread):
  2.  
  3.     def __init__ (self, debug, resultslist):
  4.  
  5.         threading.Thread.__init__(self)        
  6.  
  7.         self.debug = debug
  8.         self.resultslist = resultslist
  9.  
  10.     def run ( self ) :
  11.          # some stuff
  12.          items = ('stuff','morestuff','otherstuff')
  13.          self.resultslist.append(items)
  14.  
I was expecting to be able to populate my dictionary lists within CpuMonitor and then use them within the UpdateDatabase thread. However, while the local copy of CpuMonitor does create entries in it's resultslist variable, neither the main method or the UpdateDatabase thread see anything other than an empty list for each dictionary entry.

The same problem occurs with the debug variable. Setting debug to true within the CpuMonitor thread turns on debug for CpuMonitor, but not the rest of the program.

This leads me to believe that the way I've built the __init__ method is creating copies of the variables rather than pointers to the originals. Is that the case? If so, how to I achieve what I'm trying to do (preferrably without using global variables)?

Many thanks,
Matt.
Apr 30 '07 #1
Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Hi,

I'm new to python, and having problems instantiating classes. My main method has a couple of lines like this:

debug = False
stats_d = {"cpu":[],"foo":[],"bar":[],"baz":[]}
cpu.CpuMonitor(debug,stats_d["cpu"]).start()
db.UpdateDatabase(debug,stats_d).start()

And CpuMonitor looks like this:

class CpuMonitor (threading.Thread):

def __init__ (self, debug, resultslist):

threading.Thread.__init__(self)

self.debug = debug
self.resultslist = resultslist

def run ( self ) :
# some stuff
items = ('stuff','morestuff','otherstuff')
self.resultslist.append(items)

I was expecting to be able to populate my dictionary lists within CpuMonitor and then use them within the UpdateDatabase thread. However, while the local copy of CpuMonitor does create entries in it's resultslist variable, neither the main method or the UpdateDatabase thread see anything other than an empty list for each dictionary entry.

The same problem occurs with the debug variable. Setting debug to true within the CpuMonitor thread turns on debug for CpuMonitor, but not the rest of the program.

This leads me to believe that the way I've built the __init__ method is creating copies of the variables rather than pointers to the originals. Is that the case? If so, how to I achieve what I'm trying to do (preferrably without using global variables)?

Many thanks,
Matt.
It seems to me you should instantiate your CpuMonitor class before calling a Thread object method. Test:
Expand|Select|Wrap|Line Numbers
  1. import threading
  2.  
  3. class CpuMonitor (threading.Thread):
  4.  
  5.     def __init__ (self, debug, resultslist):
  6.  
  7.         threading.Thread.__init__(self)        
  8.  
  9.         self.debug = debug
  10.         self.resultslist = resultslist
  11.  
  12.     def run (self):
  13.          # some stuff
  14.          items = ('stuff','morestuff','otherstuff')
  15.          self.resultslist.append(items)
  16.  
  17. debug = False
  18. stats_d = {"cpu":[],"foo":[],"bar":[],"baz":[]}
  19. t = CpuMonitor(debug,stats_d["cpu"])
  20. t.start()
  21. #UpdateDatabase(debug,stats_d).start()
  22. for key in t.__dict__:
  23.     print '%s = %s' % (key, t.__dict__[key])
  24.  
  25. '''
  26. >>> _Thread__block = <Condition(<thread.lock object at 0x0094E380>, 0)>
  27. _Thread__name = Thread-5
  28. _Thread__daemonic = False
  29. debug = False
  30. _Thread__started = True
  31. _Thread__stderr = <pywin.framework.interact.DockedInteractiveView instance at 0x00D76300>
  32. _Thread__target = None
  33. resultslist = [('stuff', 'morestuff', 'otherstuff')]
  34. _Thread__kwargs = {}
  35. _Verbose__verbose = False
  36. _Thread__args = ()
  37. _Thread__stopped = True
  38. _Thread__initialized = True
  39. >>> t.resultslist
  40. [('stuff', 'morestuff', 'otherstuff')]
  41. >>> stats_d
  42. {'baz': [], 'foo': [], 'bar': [], 'cpu': [('stuff', 'morestuff', 'otherstuff')]}
  43. >>>
  44. '''
Your global dictionary 'stats_d' is updated. Is this what you expected?
When posting code, please use code tags (see reply guidelines).
Apr 30 '07 #2

bartonc
Expert 5K+
P: 6,596
Hi,

I'm new to python, and having problems instantiating classes. My main method has a couple of lines like this:
Expand|Select|Wrap|Line Numbers
  1.     debug = False
  2.     stats_d = {"cpu":[],"foo":[],"bar":[],"baz":[]}
  3.     cpu.CpuMonitor(debug,stats_d["cpu"]).start()
  4.     db.UpdateDatabase(debug,stats_d).start()
  5.  
And CpuMonitor looks like this:
Expand|Select|Wrap|Line Numbers
  1. class CpuMonitor (threading.Thread):
  2.  
  3.     def __init__ (self, debug, resultslist):
  4.  
  5.         threading.Thread.__init__(self)        
  6.  
  7.         self.debug = debug
  8.         self.resultslist = resultslist
  9.  
  10.     def run ( self ) :
  11.          # some stuff
  12.          items = ('stuff','morestuff','otherstuff')
  13.          self.resultslist.append(items)
  14.  
I was expecting to be able to populate my dictionary lists within CpuMonitor and then use them within the UpdateDatabase thread. However, while the local copy of CpuMonitor does create entries in it's resultslist variable, neither the main method or the UpdateDatabase thread see anything other than an empty list for each dictionary entry.

The same problem occurs with the debug variable. Setting debug to true within the CpuMonitor thread turns on debug for CpuMonitor, but not the rest of the program.

This leads me to believe that the way I've built the __init__ method is creating copies of the variables rather than pointers to the originals. Is that the case? If so, how to I achieve what I'm trying to do (preferrably without using global variables)?

Many thanks,
Matt.
The ONLY thread-safe way to pass data between threads is to use a queue.Queue.
Create the Queue in the main thread and pass it to you backgroud task.
In the background task use myQueue (made up name for the instance of Queue) .put(data).
In the main thread, use theQueue.get().

I'd post some actual code, but I'm currently without a machine that has both internet and python.

Hope that helps,
Barton
Apr 30 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.