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?
SP Threads?
Re: SP Threads?
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
These two scripts/plugins use GameThread:
viewtopic.php?f=17&t=1464
viewtopic.php?f=20&t=1323
Re: SP Threads?
Awesome!
This project has so many cool features, but the documentation is a bit lacking. Is there a place I can contribute to that?
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?
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?
I went ahead and made a decorator so i can just tag up things that will block in my plugins
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
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?
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?
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?
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
https://github.com/Source-Python-Dev-Te ... ick.py#L73
Re: SP Threads?
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 116 guests