-
Notifications
You must be signed in to change notification settings - Fork 815
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1457 from Textualize/win-sleep
Win sleep
- Loading branch information
Showing
6 changed files
with
76 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "textual" | ||
version = "0.9.0" | ||
version = "0.9.1" | ||
homepage = "https://github.com/Textualize/textual" | ||
description = "Modern Text User Interface framework" | ||
authors = ["Will McGugan <[email protected]>"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import ctypes | ||
from ctypes.wintypes import LARGE_INTEGER | ||
from time import sleep as time_sleep | ||
|
||
__all__ = ["sleep"] | ||
|
||
kernel32 = ctypes.windll.kernel32 | ||
|
||
INFINITE = 0xFFFFFFFF | ||
WAIT_FAILED = 0xFFFFFFFF | ||
CREATE_WAITABLE_TIMER_HIGH_RESOLUTION = 0x00000002 | ||
|
||
|
||
def sleep(sleep_for: float) -> None: | ||
"""A replacement sleep for Windows. | ||
Python 3.11 added a more accurate sleep. This may be used on < Python 3.11 | ||
Args: | ||
sleep_for (float): Seconds to sleep for. | ||
""" | ||
handle = kernel32.CreateWaitableTimerExW( | ||
None, | ||
None, | ||
CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, | ||
0x1F0003, | ||
) | ||
if not handle: | ||
time_sleep(sleep_for) | ||
return | ||
|
||
sleep_for -= 1 / 1000 | ||
if not kernel32.SetWaitableTimer( | ||
handle, | ||
ctypes.byref(LARGE_INTEGER(int(sleep_for * -10_000_000))), | ||
0, | ||
None, | ||
None, | ||
0, | ||
): | ||
kernel32.CloseHandle(handle) | ||
print("error") | ||
time_sleep(sleep_for) | ||
return | ||
|
||
if kernel32.WaitForSingleObject(handle, INFINITE) == WAIT_FAILED: | ||
time_sleep(sleep_for) | ||
kernel32.CloseHandle(handle) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters