增加多线程查询

This commit is contained in:
Jalin
2019-01-06 17:02:36 +08:00
parent 85fb384b0a
commit 0553dc936b
12 changed files with 253 additions and 27 deletions

View File

@@ -1,26 +1,45 @@
import os
import sys
from py12306.helpers.func import *
class BaseLog:
logs = []
thread_logs = {}
quick_log = []
@classmethod
def add_log(cls, content):
self = cls()
self.logs.append(content)
# print('添加 Log 主进程{} 进程ID{}'.format(is_main_thread(), current_thread_id()))
if is_main_thread():
self.logs.append(content)
else:
tmp_log = self.thread_logs.get(current_thread_id(), [])
tmp_log.append(content)
self.thread_logs[current_thread_id()] = tmp_log
return self
@classmethod
def flush(cls, sep='\n', end='\n', file=None):
self = cls()
logs = self.quick_log if self.quick_log else self.logs
if self.quick_log:
logs = self.quick_log
else:
if is_main_thread():
logs = self.logs
else:
logs = self.thread_logs[current_thread_id()]
# for i in logs:
print(*logs, sep=sep, end=end, file=file)
if self.quick_log:
self.quick_log = []
else:
self.logs = []
if is_main_thread():
self.logs = []
else:
del self.thread_logs[current_thread_id()]
# print(self.logs)
@classmethod
@@ -28,3 +47,9 @@ class BaseLog:
self = cls()
self.quick_log.append(content)
return self
def notification(self, title, content=''):
if sys.platform == 'darwin':
os.system(
'osascript -e \'tell app "System Events" to display notification "{content}" with title "{title}"\''.format(
title=title, content=content))