Python - 退出 |在进程退出时计算函数关键字参数内的全局时间
Python - atexit | Calculating global time inside function keyword arguments on process exit
当我使用def foo(time = calculate_time()): ...
时,参数(time
)为0
I suppose that, keyword argument assignment or atexit is different process than main process, so time is irrelevant
from time import time as get_time()
start_time = get_time() # Calculated at the time the application starts
def calculate_time():
global start_time
return get_time() - start_time
...
# Set the function which executed on exit
atexit.register(save_to_file)
请查看我的 KeyLogger 脚本代码中的 注释行 :
Sample problematic case:
- Run python script
- Immediatly click END button
- Floated by zero exception while executing
atexit.register(save_to_file)
- Line:
lines.append(f"Saniye başı tuş basımı (key/s): {len(pressedKeys) / passing_time}")
from pynput import keyboard
import atexit
from time import time as get_time
from datetime import datetime
LOG_FILE = "keyLog.txt"
DELIM = "|"
TIME_LIMIT = 20 * 60
start_time = get_time()
pressedKeys = []
def calculate_time():
global start_time
return get_time() - start_time
# Why calculate_time() returns 0
def save_to_file(passing_time = calculate_time()):
global pressedKeys
if passing_time is None:
passing_time = calculate_time() # Why calculate_time() doesn't return
with open(LOG_FILE, "a+", encoding="utf-8") as file:
lines = []
lines.append(f"\n\n\n\n")
lines.append(f"Tarih (Yıl-Ay-Gün Saat-Dakika-Saniye.): {datetime.now()}")
lines.append(f"Geçen süre (s): {passing_time}")
lines.append(f"Basılan karakter: {len(pressedKeys)}")
lines.append(f"Saniye başı tuş basımı (key/s): {len(pressedKeys) / passing_time}")
lines.append(f"\n")
lines.append("|".join(pressedKeys))
file.write("\n".join(lines))
# Set the function which executed on exit
atexit.register(save_to_file)
# Kill process when 'END' is clicked
def on_press(key):
global pressedKeys
char = None
try:
char = key.char
except AttributeError:
char = str(key)
pressedKeys.append(char)
time = calculate_time()
if time > TIME_LIMIT:
save_to_file(time)
def on_release(key):
print("")
if key == keyboard.Key.end:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
关键字参数的默认值是在函数定义时计算的,而不是在执行时计算的。
time.time
最多具有底层 OS 的精度。它正在尝试计算分配列表和定义函数所花费的时间,预计这会向下舍入为零(需要微秒)。
只需将默认值更改为 None
,您的函数中的 if 将计算调用时的时间差。
当我使用def foo(time = calculate_time()): ...
时,参数(time
)为0
I suppose that, keyword argument assignment or atexit is different process than main process, so time is irrelevant
from time import time as get_time()
start_time = get_time() # Calculated at the time the application starts
def calculate_time():
global start_time
return get_time() - start_time
...
# Set the function which executed on exit
atexit.register(save_to_file)
请查看我的 KeyLogger 脚本代码中的 注释行 :
Sample problematic case:
- Run python script
- Immediatly click END button
- Floated by zero exception while executing
atexit.register(save_to_file)
- Line:
lines.append(f"Saniye başı tuş basımı (key/s): {len(pressedKeys) / passing_time}")
from pynput import keyboard
import atexit
from time import time as get_time
from datetime import datetime
LOG_FILE = "keyLog.txt"
DELIM = "|"
TIME_LIMIT = 20 * 60
start_time = get_time()
pressedKeys = []
def calculate_time():
global start_time
return get_time() - start_time
# Why calculate_time() returns 0
def save_to_file(passing_time = calculate_time()):
global pressedKeys
if passing_time is None:
passing_time = calculate_time() # Why calculate_time() doesn't return
with open(LOG_FILE, "a+", encoding="utf-8") as file:
lines = []
lines.append(f"\n\n\n\n")
lines.append(f"Tarih (Yıl-Ay-Gün Saat-Dakika-Saniye.): {datetime.now()}")
lines.append(f"Geçen süre (s): {passing_time}")
lines.append(f"Basılan karakter: {len(pressedKeys)}")
lines.append(f"Saniye başı tuş basımı (key/s): {len(pressedKeys) / passing_time}")
lines.append(f"\n")
lines.append("|".join(pressedKeys))
file.write("\n".join(lines))
# Set the function which executed on exit
atexit.register(save_to_file)
# Kill process when 'END' is clicked
def on_press(key):
global pressedKeys
char = None
try:
char = key.char
except AttributeError:
char = str(key)
pressedKeys.append(char)
time = calculate_time()
if time > TIME_LIMIT:
save_to_file(time)
def on_release(key):
print("")
if key == keyboard.Key.end:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
关键字参数的默认值是在函数定义时计算的,而不是在执行时计算的。
time.time
最多具有底层 OS 的精度。它正在尝试计算分配列表和定义函数所花费的时间,预计这会向下舍入为零(需要微秒)。
只需将默认值更改为 None
,您的函数中的 if 将计算调用时的时间差。