SP Threads?

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

SP Threads?

Postby Zeus » Sat Mar 24, 2018 9:49 pm

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?
User avatar
VinciT
Senior Member
Posts: 331
Joined: Thu Dec 18, 2014 2:41 am

Re: SP Threads?

Postby VinciT » Sat Mar 24, 2018 11:27 pm

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
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: SP Threads?

Postby Zeus » Sun Mar 25, 2018 12:34 am

Awesome!

This project has so many cool features, but the documentation is a bit lacking. Is there a place I can contribute to that?
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: SP Threads?

Postby satoon101 » Sun Mar 25, 2018 1:28 am

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
Image
User avatar
Zeus
Member
Posts: 52
Joined: Sat Mar 24, 2018 5:25 pm
Location: Chicago
Contact:

Re: SP Threads?

Postby Zeus » Sun Mar 25, 2018 4:25 am

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
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: SP Threads?

Postby Ayuto » Sun Mar 25, 2018 9:16 am

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.
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: SP Threads?

Postby Doldol » Sun Mar 25, 2018 10:51 am

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!?
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: SP Threads?

Postby Ayuto » Sun Mar 25, 2018 11:18 am

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
User avatar
Doldol
Senior Member
Posts: 200
Joined: Sat Jul 07, 2012 7:09 pm
Location: Belgium

Re: SP Threads?

Postby Doldol » Mon Mar 26, 2018 11:36 am

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!

Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 34 guests