Small "threaded" function decorator

All other Source.Python topics and issues.
User avatar
Posts: 51
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago

Small "threaded" function decorator

Postby Zeus » Mon Jun 29, 2020 6:23 am

Code: Select all

from listeners.tick import GameThread

def threaded(fn):
    def wrapper(*args, **kwargs):
        thread = GameThread(target=fn, args=args, kwargs=kwargs)
        thread.daemon = True
    return wrapper

def load():

def do_something_that_blocks():

Something I've run into a bunch of times is needing something run without blocking the main game thread like external HTTP calls to the Discord API. Since network calls, especially slow ones will cause the game to wait until it's complete; this allows me to keep the game going while doing what I need to on the backend.

Of course you will not be able to get a value returned directly from the call; instead you'll have to manage that outside of the function. This works fine for my use case since it's fire and forget.

Maybe worth adding something like this to SP? Unless theres a simplier pattern that can allow one to not block the game thread.

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 6 guests