namespace & imported modules

Jason gaudette at ele.uri.edu
Wed Nov 24 16:55:08 EST 2004


Thanks for your quick reply, Peter.

I had to dust off my Learning Python book, which I had read cover to
cover many months ago.  After re-reading some key sections I now
understand what 'global' scope really means and why you suggested
importing the module directly, rather than copying the namespace with
the 'from' statement.

When importing the module directly as 'import module' in lieu of 'from
module import *', this doesn't seem to address the issue I'm having.

My main program now imports the submodule names that must be qualified
with the module name, but the function in one submodule still can't
see the function (class instance method to be exact) defined in the
other submodule.

I suppose I can post the test code I was using.  The multithreaded
hierarchy mimics that of my 'real' program that I need to integrate my
code into, so don't let this complexity confuse you.

===========================================================
# main.py
#
#!/usr/bin/env python
#
# A customized class to terminate a thread using the threading
module's event-
#   based functionality.
#
###############################################################################

import threading, time

from hb_imports import *
import hb_imports
import hb_global

def main():
	# launch WatchDog timer thread
	bcast_watchdog = hb_global.WatchDog(timeout=3)
	bcast_watchdog.setDaemon(1)
	bcast_watchdog.start()
	
	# launch global arbiter
	glob_arbiter = threading.Thread(target=hb_global.global_arbiter)
	glob_arbiter.setDaemon(1)
	glob_arbiter.start()
	
	time.sleep(7)
	cfg['ISROOT'] = False		# simulation: no longer root again
	
	time.sleep(7)

if __name__ == '__main__':
	main()

===========================================================
# hb_global.py

import threading, time
import hb_stethoscope
from hb_imports import *

class WatchDog(threading.Thread):
	def __init__(self, name='WatchDog', timeout=1.0):
		self._timerevent = threading.Event()	# assign event to instance
		self._waitperiod = timeout 	# assign timeout delay to instance
variable

		threading.Thread.__init__(self, name=name)
	
	def run(self):
		while 1:
			while not cfg['ISROOT']:
				print now() + 'Timer started. %s seconds to receive ping.' %
self._waitperiod
				
				# initialize timer and flags
				self._timerevent.clear()		# BE KIND. REWIND.
				self._intervened = False		# init flag denoting intervention
occurred
				
				# wait for reset() method to be called or timeout
				self._timerevent.wait(self._waitperiod)
					
				# if _intervened flag not set AND still not a root server (status
could change in countdown)
				if not self._intervened and not cfg['ISROOT']:
					print now() + "No broadcasts received in %s seconds! Becoming a
root server." % (self._waitperiod,)
					cfg['ISROOT'] = True		# modify root status

			time.sleep(0.1)			# sleep 100ms to avoid constant polling
	
	def reset(self):
		self._intervened = True
		self._timerevent.set()


def global_arbiter():
	threading.Thread(target=hb_stethoscope.stethoscope).start()

===========================================================
# hb_stethoscope.py

import threading, time
import hb_global
from hb_imports import *

def stethoscope():
	for i in range(6):
		if i == 3:
			time.sleep(4)
		sim_delay = 1
		time.sleep(sim_delay)
		threading.Thread(target=bcast_handle).start()

def bcast_handle():
	print now() + 'Receiving broadcast ping. Reseting watchdog timer.'
	hb_global.bcast_watchdog.reset()

===========================================================
# hb_imports.py

import time

otime = time.time()

def now():
	ctime = time.time()
	return '%.2f :: ' % (ctime-otime,)

cfg = dict()
cfg['ISROOT'] = False

===========================================================

The last line in hb_stethoscope.py, hb_global.bcast_watchdog.reset(),
is _supposed_ to call the WatchDog.reset() class instance method in
hb_global.py.

I've been very frustrated trying to find out why I can't access this
method.

Thanks for anyone taking a look at this problem.

Happy Thanksgiving,

Jason



More information about the Python-list mailing list