如何在下载期间禁用暂停功能,但屏幕仍会关闭?

如何在下载期间禁用暂停功能,但屏幕仍会关闭?

我在 Ubuntu Gnome 15.10 上。

我经常下载大文件(约 800 MB),但我的互联网连接速度很慢。下载通常需要相对较长的时间。由于我不在笔记本电脑上持续工作,它会自动进入睡眠/休眠模式。

当我在它进入睡眠/休眠模式后按下任意键时,它会唤醒并带我进入登录屏幕。

我找到了电源设置,但令人惊讶的是,它提供的选项很少:

电源选项

不过,有几件事我不确定

有没有办法让我的电脑在下载过程中不挂起,但在下载期间我不在操作时屏幕仍会关闭?是否有设置可以实现此目的,或者有其他解决方案?

答案1

在被骗的边缘这个问题。这个问题的不同之处在于,你希望计算机不挂起,但在下载大文件时屏幕仍然关闭。虽然看似差别很小,但它对答案(脚本)产生实质性的影响。

关于解决方案

  • 解决方案是背景脚本作为启动应用程序运行,这样在下载过程中就会禁用暂停功能。
  • 与此同时,第二线程在脚本内部,借助 来跟踪空闲时间xprintidlexprintidle由键盘和鼠标事件触发。在脚本头部设置的任意时间之后,线程会关闭屏幕,而另一个(主)线程会在下载处于活动状态时阻止暂停。

    通过使用 定期检查,可以通过下载文件夹大小的变化来判断下载是否du -ks ~/Downloads完成;如果文件夹大小在五分钟内没有再发生变化,则脚本认为下载已完成。然后重新启用暂停。

笔记

  • 与往常一样(-应该如此),使用后台脚本时,额外的处理器负载为零。

剧本

#!/usr/bin/env python3
import subprocess
import time
from threading import Thread

def get_size():
    return int(subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8").split()[0])

def get(cmd):
    return subprocess.check_output(["/bin/bash", "-c", cmd]).decode("utf-8")


#--- set suspend time below (seconds)
suspend_wait = 300 # = 5 minutes
#--- set time after which screen should blackout (seconds)
blackout = 300 # = 5 minutes


#--- you can change values below, but I'd leave them as they are
speed_limit = 0      # set a minimum speed (kb/sec) to be considered a download activity
looptime = 20        # time interval between checks
download_wait = 300  # time (seconds) to wait after the last download activity before suspend is re- activated
#---

t = 0
key = ["gsettings", "get", "org.gnome.settings-daemon.plugins.power", "sleep-inactive-ac-timeout", "set"]
set_suspend = key[0]+" "+key[-1]+" "+(" ").join(key[2:4])
get_suspend = (" ").join(key[0:4])
check_1 = int(get("du -ks ~/Downloads").split()[0])

def get_idle(blackout):
    shutdown = False
    while True:
        curr_idle = int(subprocess.check_output(["xprintidle"]).decode("utf-8").strip())/1000
        time.sleep(10)
        if curr_idle > blackout:
            if shutdown == False:
                subprocess.Popen(["xset", "-display", ":0.0", "dpms", "force", "off"])
                shutdown = True
                print("shutting down")
            else:
                pass
        else:
            shutdown = False

Thread(target=get_idle, args=(blackout,)).start()

while True:
    time.sleep(looptime)
    try:
        check_2 = int(get("du -ks ~/Downloads").split()[0])
    except subprocess.CalledProcessError:
        pass
    speed = int((check_2 - check_1)/looptime)
    # check current suspend setting
    suspend = get(get_suspend).strip()
    if speed > speed_limit:
        # check/set disable suspend if necessary
        if suspend != "0":
            subprocess.Popen(["/bin/bash", "-c", set_suspend+" 0"])
        t = 0
    else:
        if all([t > download_wait/looptime, suspend != str(download_wait)]):
            # check/enable suspend if necessary
            subprocess.Popen(["/bin/bash", "-c", set_suspend+" "+str(suspend_wait)])
    check_1 = check_2
    t = t+1


如何使用

  1. 该脚本使用xprintidle

    sudo apt-get install xprintidle
    
  2. 将以下脚本复制到一个空文件中,并将其另存为no_suspend.py

  3. 在脚本的头部,设置所需的“正常”挂起时间(因为脚本将重新启用挂起功能)以及您希望屏幕关闭的时间:

    #--- set suspend time below (seconds)
    suspend_wait = 300 # = 5 minutes
    #--- set time after which screen should blackout (seconds)
    blackout = 300 # = 5 minutes
    
  4. 如果你愿意,你设置其他值:

    #--- you can change values below, but I'd leave them as they are
    speed_limit = 0      # set a minimum speed (kb/sec) to be considered a download activity
    looptime = 20        # time interval between checks (in seconds)
    download_wait = 300  # time (seconds) to wait after the last download activity before suspend is re- activated
    #---
    
  5. 使用以下命令测试运行脚本:

    python3 /path/to/no_suspend.py
    
  6. 如果一切正常,请将其添加到您的启动应用程序:Dash > 启动应用程序> 添加命令:

    python3 /path/to/no_suspend.py
    

相关内容