Page 1 of 1

SP Threads?

Posted: Sat Mar 24, 2018 9:49 pm
by Zeus
First off, this project is amazing, really awesome work so far!

My question may be a bit in depth; but how does SourcePython work in terms of the running srcds process. From my toying around, I've noticed that SourcePython seems to work in a single thread, which is on a game thread.

Is it possible to do things like HTTP or other slow / blocking operations without interupting the game?

Re: SP Threads?

Posted: Sat Mar 24, 2018 11:27 pm
by VinciT
Yeah, you can use GameThread (subclass of Python's threading.Thread) to avoid freezing the server.

These two scripts/plugins use GameThread:
viewtopic.php?f=17&t=1464
viewtopic.php?f=20&t=1323

Re: SP Threads?

Posted: Sun Mar 25, 2018 12:34 am
by Zeus
Awesome!

This project has so many cool features, but the documentation is a bit lacking. Is there a place I can contribute to that?

Re: SP Threads?

Posted: Sun Mar 25, 2018 1:28 am
by satoon101
Zeus wrote:Awesome!

This project has so many cool features, but the documentation is a bit lacking. Is there a place I can contribute to that?

Certainly!
http://wiki.sourcepython.com/contributing/contributing.html#contributing-to-the-wiki

Re: SP Threads?

Posted: Sun Mar 25, 2018 4:25 am
by Zeus
I went ahead and made a decorator so i can just tag up things that will block in my plugins

Code: Select all

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


Code: Select all

@Event('player_say')
@threaded
def on_player_say(event):
    player = Player.from_userid(event['userid'])

    msg = f"**{player.name}**: {event['text']}"
    response = discord.create_message("123456789", {"content": msg})
 


Idk if that snippet is useful to anyone else :)

Though one of the things I really love Gevent is that it'll patch the stdlib for all blocking calls and make them cooperative. Wondering is SP could implement Gevent into it's core functionality. I would like the imperative interface while not blocking on the game thread. But i realize that would be an intense amount of refactor so /shrug

Re: SP Threads?

Posted: Sun Mar 25, 2018 9:16 am
by Ayuto
In the past it was necessarry to use GameThread instead of threading.Thread as we had to implement some fixes to make threads work correctly in dedicated servers. Today, this isn't required anymore, but GameThread still has a nice feature. It will print a warning if your plugin is unloaded while your thread is still running.

Re: SP Threads?

Posted: Sun Mar 25, 2018 10:51 am
by Doldol
Ayuto wrote:In the past it was necessarry to use GameThread instead of threading.Thread as we had to implement some fixes to make threads work correctly in dedicated servers. Today, this isn't required anymore, but GameThread still has a nice feature. It will print a warning if your plugin is unloaded while your thread is still running.


Does that mean I don't have to register a tick listener anymore to make sure the thread doesn't pause!?

Re: SP Threads?

Posted: Sun Mar 25, 2018 11:18 am
by Ayuto
You never had to do that when using GameThread as that was the whole purpose of that class. However, threading.Thread is now working fine, because SP always has a global tick listener registered:
https://github.com/Source-Python-Dev-Te ... ick.py#L73

Re: SP Threads?

Posted: Mon Mar 26, 2018 11:36 am
by Doldol
Ayuto wrote:You never had to do that when using GameThread as that was the whole purpose of that class. However, threading.Thread is now working fine, because SP always has a global tick listener registered:
https://github.com/Source-Python-Dev-Te ... ick.py#L73

Great!