Skip to content

Commit

Permalink
优化:解决 input()jobs -f 中的阻塞问题 (#17 , #24 , #31)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachpt committed Jun 7, 2020
1 parent 58c7384 commit 1724cc3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
74 changes: 51 additions & 23 deletions cloud189/cli/manager.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,45 @@
from time import sleep
from threading import Thread
from time import sleep, monotonic

from cloud189.cli.downloader import TaskType
from cloud189.cli.utils import info, error, get_file_size_str
from cloud189.cli.utils import info, error, get_file_size_str, OS_NAME
from cloud189.cli.reprint import output # 修改了 magic_char

__all__ = ['global_task_mgr']

output_list = output()
total_tasks = 0

def input_with_timeout(timeout=1.5):
"""带超时的 input"""
input_with_timeput_ans = None
def foo():
global input_with_timeput_ans
input_with_timeput_ans = input()

thd = Thread(target=foo)
thd.daemon = True
thd.start()
sleep(timeout)
return input_with_timeput_ans
class TimeoutExpired(Exception):
pass


def input_with_timeout(timeout, timer=monotonic):
if OS_NAME == 'posix': # *nix
import signal

def alarm_handler(signum, frame):
raise TimeoutExpired

signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(timeout)
try:
return input()
finally:
signal.alarm(0)
else: # windos
import msvcrt

endtime = timer() + timeout
result = []
while timer() < endtime:
if msvcrt.kbhit():
result.append(msvcrt.getwche())
if result[-1] == '\n' or result[-1] == '\r':
return ''.join(result[:-1])
sleep(0.05) # 这个值太大会导致丢失按键信息
raise TimeoutExpired


class TaskManager(object):
Expand Down Expand Up @@ -64,6 +84,7 @@ def add_task(self, task):

@staticmethod
def _size_to_msg(now_size, total_size, msg, pid, task) -> (str, bool):
"""finish 仅用于标识秒传于失败任务,它们没有 size 信息"""
if total_size == -1:
percent = get_file_size_str(now_size)
else:
Expand Down Expand Up @@ -112,30 +133,37 @@ def _show_task(pid, task, follow=False):

def stop_show_task():
"""停止显示任务状态"""
stop_signal = None
while TaskManager.running or total_tasks > 0:
stop_signal = input_with_timeout()
if stop_signal:
TaskManager.running = False
sleep(1)
try:
stop_signal = input_with_timeout(3)
except TimeoutExpired:
pass
else:
if stop_signal:
TaskManager.running = False
break

if follow: Thread(target=stop_show_task).start()
now_size, total_size, msg = task.get_process()
done_files, total_files = task.get_count()
while total_tasks > 1 or total_size == -1 or now_size < total_size or done_files < total_files:
# total_tasks 用于标记还没完成的任务数量,只有还有一个没有完成,其他已经完成了的也要等待
while total_tasks > 0 or total_size == -1 or now_size < total_size or done_files < total_files:
if not TaskManager.running:
break
break # 用户中断
result, finished = TaskManager._size_to_msg(now_size, total_size, msg, pid, task)
if follow:
output_list[pid] = result
sleep(1)
now_size, total_size, msg = task.get_process()
done_files, total_files = task.get_count()
if now_size >= total_size:
if now_size >= total_size and done_files >= total_files:
total_tasks -= 1
break
else:
break
if finished: # 文件秒传没有大小
break
break # 非实时显示模式,直接结束
if finished:
break # 文件秒传、出错 没有大小
if now_size >= total_size:
result, _ = TaskManager._size_to_msg(now_size, total_size, msg, pid, task)
output_list[pid] = result
Expand Down
9 changes: 5 additions & 4 deletions cloud189/cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
GIT_REPO = "Aruelius/cloud189"
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
M_platform = platform()
OS_NAME = os.name


def error(msg):
Expand Down Expand Up @@ -79,10 +81,9 @@ def captcha_handler(img_data):
img_path = os.path.dirname(sys.argv[0]) + os.sep + 'captcha.png'
with open(img_path, 'wb') as f:
f.write(img_data)
m_platform = platform()
if m_platform == 'Darwin':
if M_platform == 'Darwin':
os.system(f'open {img_path}')
elif m_platform == 'Linux':
elif M_platform == 'Linux':
# 检测是否运行在没有显示屏的 console 上
if os.environ.get('DISPLAY'):
os.system(f'xdg-open {img_path}')
Expand Down Expand Up @@ -192,7 +193,7 @@ def print_logo():

def print_help():
# clear_screen()
help_text = f""" cloud189-cli | 天翼云盘客户端 for {sys.platform} | v{version}
help_text = f""" cloud189-cli | 天翼云盘客户端 for {M_platform} | v{version}
• 支持文件秒传,文件夹保持相对路径上传
• 获取文件分享链接,批量上传下载,断点续传等功能
Expand Down

2 comments on commit 1724cc3

@Aruelius
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

@rachpt
Copy link
Collaborator Author

@rachpt rachpt commented on 1724cc3 Jun 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f2ad94c 应该可以了,之前只在 window 上测试了没有在 Linux 上测试

Please sign in to comment.