Last active
July 17, 2025 15:28
-
-
Save James-E-A/478576313ae6d76bf0ef04001b557398 to your computer and use it in GitHub Desktop.
Python run a block of code in a background thread immediately
This file contains hidden or 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
import threading | |
def thread_start_immed(*a, _thread_daemon=True, **k): | |
"""Usage: | |
@thread_start_immed("my background thread", _thread_daemon=False) | |
def t(name): | |
import time | |
time.sleep(3) | |
print(f"Hello from {name}!") | |
print("Started background thread:", t) | |
""" | |
return lambda f: _start_thread(target=f, args=a, kwargs=k, daemon=_thread_daemon) | |
def _start_thread(*a, **k): | |
t = threading.Thread(*a, **k) | |
t.start() | |
return t |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment