build_index now in separate thread

This commit is contained in:
Roel 2014-04-24 17:03:42 +02:00
parent 70711312b0
commit 078ca154ff

41
main.py
View File

@ -21,9 +21,6 @@ class Meshenger:
os.mkdir(self.msg_dir) os.mkdir(self.msg_dir)
print 'Making message directory' print 'Making message directory'
print 'Building own index for the first time\n'
self.build_index()
try: try:
d = threading.Thread(target=self.discover) d = threading.Thread(target=self.discover)
d.daemon = True d.daemon = True
@ -37,10 +34,16 @@ class Meshenger:
s.daemon = True s.daemon = True
s.start() s.start()
b = threading.Thread(target=self.build_index)
b.daemon = True
b.start()
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
print 'exiting discovery thread' print 'exiting discovery thread'
d.join() d.join()
a.join() a.join()
b.join()
s.join()
sys.exit() sys.exit()
while True: while True:
@ -64,8 +67,8 @@ class Meshenger:
self.get_index(device, nodepath) self.get_index(device, nodepath)
print 'downloading messages' print 'downloading messages'
self.get_messages(device, nodepath) self.get_messages(device, nodepath)
print 'updating own index' #print 'updating own index'
self.build_index() #self.build_index()
time.sleep(5) #free process or ctrl+c time.sleep(5) #free process or ctrl+c
@ -131,25 +134,31 @@ Initialize the server
Make an index file of all the messages present on the node. Make an index file of all the messages present on the node.
Save the time of the last update. Save the time of the last update.
""" """
print 'Building own index for the first time\n'
if not os.path.exists('index'): if not os.path.exists('index'):
with open('index','wb') as index: with open('index','wb') as index:
index.write('') index.write('')
previous_index = [] previous_index = []
current_index = os.listdir(self.msg_dir) while not self.exitapp:
current_index = os.listdir(self.msg_dir)
if current_index != previous_index:
with open('index', 'wb') as index:
for message in os.listdir(self.msg_dir):
index.write(message)
index.write('\n')
self.index_last_update = str(int(time.time()))
print 'Index updated:', current_index
if current_index != previous_index: with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig
with open('index', 'wb') as index: indexupdate.write(str(int(time.time())))
for message in os.listdir(self.msg_dir):
index.write(message)
index.write('\n')
self.index_last_update = str(int(time.time()))
with open('index_last_update', 'wb') as indexupdate: ### misschien is dit overbodig previous_index = current_index
indexupdate.write(str(int(time.time()))) time.sleep(5)
current_index = previous_index
def get_index(self,ip, path): def get_index(self,ip, path):
""" """