[TIC-CORE] support recommends tag
[archive/20170607/tools/tic-core.git] / test / test_threadsafe.py
1 '''
2 Created on Mar 7, 2017
3
4 @author: shinchulwoo
5 '''
6
7 import os
8 import threading
9 from tic.utils.file import make_dirs, decompress_gzip, copyfile_flock
10 from tic.utils.grabber import myurlgrab2
11 import contextlib
12
13 class MyThread(threading.Thread):
14     def __init__(self, name, url):
15         threading.Thread.__init__(self)
16         self.name = name
17         self.url = url
18         self.__suspend = False
19         self.__exit = False
20         self.__path = './test/thread/'
21         self.__cachepath = './test/thread/cache'
22     def run(self):
23         print('%s Thread start !!!' % self.name)
24         path=os.path.join(self.__path, self.name)
25         make_dirs(path)
26         filename=os.path.join(path, os.path.basename(self.url))
27         myurlgrab2(self.url, filename)
28         print('%s Thread download file !!!' % self.name)
29         # Check if file compressed or not
30         if filename.endswith(".gz"):
31             decompress_filename = os.path.splitext(filename)[0]
32             filename = decompress_gzip(filename, decompress_filename)
33         print('%s Thread decompress file !!!' % self.name)
34         
35         cachefile = os.path.join(self.__cachepath, os.path.basename(filename))
36         copyfile_flock(filename, cachefile)
37         print('%s Thread finish !!!' % self.name)
38         
39     def mySuspend(self):
40         self.__suspend = True
41     def myResume(self):
42         self.__suspend = False
43     def myExit(self):
44         self.__exit = True
45
46 if __name__ == '__main__':
47     url = ''
48     th1 = MyThread('th1', url)
49     th1.start()
50 #     th2 = MyThread('th2', url)
51 #     th2.start()