import random
import threading
import time
import logging
import urllib
#import PyTango
#from PyTango.gevent import DeviceProxy
#from PyTango import DeviceProxy
import tango
from tango.gevent import DeviceProxy

logging.basicConfig(level=logging.DEBUG,
                    format=' %(asctime)s %(threadName)-14s: %(message)s',
                    )

def worker():
    """thread worker function"""
    #for i in range(10):
    #    logging.debug('connection %s'%i)
    #    f = urllib.urlopen("http://10.0.1.197")
    #    f.read()
    t = threading.currentThread()
    logging.debug("started working %s"%t.getName())
    dev = DeviceProxy('cmsserver:10000/MNC/CMC/CPX')
    logging.debug("proxy created by %s"%t.getName())
    count = 25
    while count:
        try:
            logging.debug('Reading attribute Date:')
            logging.debug(dev.Date)
        except:
            logging.debug("Exception occured")
        #time.sleep(1)
    #logging.debug('sleeping:%s'%5)
    #time.sleep(1)
    #logging.debug('ending : %s'%t.getName())

    return

for i in range(1):
    t = threading.Thread(target=worker)
    t.setDaemon(True)
    t.start()


main_thread = threading.currentThread()
print 'tango.Release.version',tango.Release.version
# print threading.enumerate()
count = 1000000
while count:
    #print 'tango.Release.version',tango.Release.version
    logging.debug('Thread: %s, count:%s'%(main_thread.getName(),count))
    count -= 1


for t in threading.enumerate():
    if t is main_thread:
        continue
    logging.debug('joining %s', t.getName())
    t.join()
