Кодом, как я могу протестировать, если жесткий диск спит, не будя его

Я бы сделал что-то вроде этого:

cli.py:

from flask import Flask
import click

def register_cli(app: Flask):
    @app.cli.command()
    @click.argument('name')
    def create_user(name):
        print("hello", name)

app.py:

from flask import Flask
from cli import register_cli

app = Flask(__name__)
register_cli(app)

Распространено создавать и настраивать (или просто настроить) app в заводские функции .

9
задан Jonx 13 May 2009 в 15:40
поделиться

1 ответ

Решение C ++ (вызовите GetDiskPowerState, и он будет перебирать физические диски, пока они не закончатся):

class AutoHandle
{
    HANDLE  mHandle;
public:
    AutoHandle() : mHandle(NULL) { }
    AutoHandle(HANDLE h) : mHandle(h) { }

    HANDLE * operator & ()
    {
        return &mHandle;
    }

    operator HANDLE() const
    {
        return mHandle;
    }

    ~AutoHandle()
    {
        if (mHandle && mHandle != INVALID_HANDLE_VALUE)
            ::CloseHandle(mHandle);
    }
};


bool
GetDiskPowerState(LPCTSTR disk, string & txt)
{
    AutoHandle hFile = CreateFile(disk, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile && hFile != INVALID_HANDLE_VALUE)
    {
        BOOL powerState = FALSE;
        const BOOL result = GetDevicePowerState(hFile, &powerState);
        const DWORD err = GetLastError();

        if (result)
        {
            if (powerState)
            {
                txt += disk;
                txt += " : powered up\r\n";
            }
            else
            {
                txt += disk;
                txt += " : sleeping\r\n";
            }
            return true;
        }
        else
        {
            txt += "Cannot get drive ";
            txt += disk;
            txt += "status\r\n";
            return false;
        }
    }

    return false;
}

string 
GetDiskPowerState()
{
    string text;
    CString driveName;
    bool result = true;
    for (int idx= 0; result; idx++)
    {
        driveName.Format("\\\\.\\PhysicalDrive%d", idx);
        result = GetDiskPowerState(driveName, text);
    }
    return text;
}
6
ответ дан 4 December 2019 в 21:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: