Skip to content

Instantly share code, notes, and snippets.

@James-E-A
Last active June 12, 2025 21:43
Show Gist options
  • Save James-E-A/478576313ae6d76bf0ef04001b557398 to your computer and use it in GitHub Desktop.
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
def _thread_start_immed(*a, **k):
"""Usage:
@_thread_start_immed("my background thread")
def t(name):
import time
time.sleep(3)
print(f"Hello from {name}!")
print("Started background thread:", t)
"""
def f(target):
t = threading.Thread(target=target, args=a, kwargs=k, daemon=True)
t.start()
return t
return f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment