-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeout support #3
Comments
As a workaround I'm able to do the following: from mcipc.query import Client
import signal
def check_players_online(host: str, port: int) -> bool:
def _timeout_handler(signum, frame):
raise Exception("Minecraft server did not reply in time")
signal.signal(signal.SIGALRM, _timeout_handler)
signal.alarm(1) # Should be instant since the server is at localhost
with Client(host, port) as client:
print(client.basic_stats)
signal.alarm(0)
return client.basic_stats.num_players > 0 However this won't necessarily work in multithreaded or non-UNIX environments. |
The Rcon an Query clients use python's |
The new timeout paramater works perfectly. Thanks for the fix! For future reference, my new usage looks as follows: def check_players_online(host: str, port: int) -> bool:
with Client(host, port, timeout=1) as client:
print(client.basic_stats)
print(f"{client.basic_stats.num_players} player(s) are online")
return client.basic_stats.num_players > 0 |
Glad, I could help. I'm always interested in providing new features. |
Thanks for the library, works great. I'm using it for a script that will automatically spin down the VPS the minecraft server is running on if no players are detected within a certain time.
However one issue I'm finding is if the server is not responding then the library blocks indefinitely. I'm using the following code to fetch the stats.
It would be nice if some optional timeout could be implemented into the client so that e.g. server being offline could be handled better.
The text was updated successfully, but these errors were encountered: