Browse Source

readme: explain how python's time.sleep works

Fabian Peter Hammerle 3 years ago
parent
commit
d75e1c7778
1 changed files with 20 additions and 0 deletions
  1. 20 0
      README.md

+ 20 - 0
README.md

@@ -0,0 +1,20 @@
+## sleep
+
+cpython's `time.sleep` calls `pysleep`:
+https://github.com/python/cpython/blob/v3.8.5/Modules/timemodule.c#L338
+
+`pysleep` calls `Sleep`, probably the one defined in `Modules/_tkinter.c`, with millisecond precision (rounding up):
+https://github.com/python/cpython/blob/v3.8.5/Modules/timemodule.c#L1873
+
+`Modules/_tkinter.c:Sleep` (https://github.com/python/cpython/blob/v3.8.5/Modules/_tkinter.c#L361):
+```c
+static void
+Sleep(int milli)
+{
+    /* XXX Too bad if you don't have select(). */
+    struct timeval t;
+    t.tv_sec = milli/1000;
+    t.tv_usec = (milli%1000) * 1000;
+    select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
+}
+```