You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					80 lines
				
				2.5 KiB
			
		
		
			
		
	
	
					80 lines
				
				2.5 KiB
			| 
											2 years ago
										 | import pymysql
 | ||
|  | from pymysql.cursors import DictCursor
 | ||
|  | from multiprocessing import cpu_count
 | ||
|  | from update.models import Cinema
 | ||
|  | import queue
 | ||
|  | import threading
 | ||
|  | 
 | ||
|  | 
 | ||
|  | class GetVersion(object):
 | ||
|  |     def __init__(self, cinema_ip_list=None):
 | ||
|  |         self.cinema_ip_list = cinema_ip_list
 | ||
|  |         self.queue = queue.Queue()  # 注册队列
 | ||
|  |         self.th_num = 0  # 获取CPU核心数量,-1后座位处理逻辑的线程数量
 | ||
|  | 
 | ||
|  |     def get_all_cinema(self):
 | ||
|  |         print('get_all_cinema')
 | ||
|  |         if self.cinema_ip_list is None:
 | ||
|  |             all_cinema_obj = Cinema.objects.all()
 | ||
|  |         else:
 | ||
|  |             all_cinema_obj = Cinema.objects.filter(ip__in=self.cinema_ip_list)
 | ||
|  |         for cinema_obj in all_cinema_obj:
 | ||
|  |             print(cinema_obj)
 | ||
|  |             data = {
 | ||
|  |                 'ip': cinema_obj.ip,
 | ||
|  |                 'db_user': cinema_obj.db_user,
 | ||
|  |                 'db_pwd': cinema_obj.db_pwd
 | ||
|  |             }
 | ||
|  |             self.queue.put(data)
 | ||
|  |         cpu_num = cpu_count() - 1 if cpu_count() > 1 else 1
 | ||
|  |         cinema_num = len(all_cinema_obj)
 | ||
|  |         self.th_num = cpu_num if cpu_num < cinema_num else cinema_num
 | ||
|  |         print(self.queue)
 | ||
|  | 
 | ||
|  |     def main_process(self):
 | ||
|  |         print('main_process')
 | ||
|  |         self.get_all_cinema()
 | ||
|  |         threads = []
 | ||
|  |         for i in range(self.th_num):
 | ||
|  |             t = threading.Thread(target=self.get_cinema_ver, args=(self.queue,))
 | ||
|  |             threads.append(t)
 | ||
|  |         for i in range(self.th_num):
 | ||
|  |             threads[i].start()
 | ||
|  |         for i in range(self.th_num):
 | ||
|  |             threads[i].join()
 | ||
|  | 
 | ||
|  |     @staticmethod
 | ||
|  |     def get_cinema_ver(q):
 | ||
|  |         print('get_cinema_ver')
 | ||
|  |         if q.empty():
 | ||
|  |             return
 | ||
|  |         else:
 | ||
|  |             cinema_config = q.get()
 | ||
|  |         print(cinema_config)
 | ||
|  |         sql_str = "SELECT * FROM cinema_version;"
 | ||
|  |         ip = cinema_config.get('ip')
 | ||
|  |         db_config = {
 | ||
|  |             'host': ip,
 | ||
|  |             'user': cinema_config.get('db_user'),
 | ||
|  |             'password': cinema_config.get('db_pwd'),
 | ||
|  |             'database': 'cine',
 | ||
|  |             'connect_timeout': 5,
 | ||
|  |         }
 | ||
|  |         print(db_config)
 | ||
|  |         db_conn = pymysql.Connect(**db_config)
 | ||
|  |         db_cursor = db_conn.cursor(cursor=DictCursor)
 | ||
|  |         db_cursor.execute(sql_str)
 | ||
|  |         res = db_cursor.fetchone()
 | ||
|  |         print(res)
 | ||
|  |         update_data = {
 | ||
|  |             'sys_ver': res['server_version'],
 | ||
|  |             'client_ver': res['client_version']
 | ||
|  |         }
 | ||
|  |         print(update_data)
 | ||
|  |         Cinema.objects.filter(ip=ip).update(**update_data)
 | ||
|  | 
 | ||
|  | 
 | ||
|  | if __name__ == '__main__':
 | ||
|  |     v = GetVersion()
 | ||
|  |     v.get_all_cinema()
 |