Giter VIP home page Giter VIP logo

Comments (4)

Tethik avatar Tethik commented on August 26, 2024

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.

from mcipc.

conqp avatar conqp commented on August 26, 2024

The Rcon an Query clients use python's socket library.
You should be able to set timeouts as described in the socket library's documentation:
https://docs.python.org/3/library/socket.html#socket-timeouts
PS: I added an additional optional timeout parameter to the Rcon and Query clients:
f7c2a3c

from mcipc.

Tethik avatar Tethik commented on August 26, 2024

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

from mcipc.

conqp avatar conqp commented on August 26, 2024

Glad, I could help. I'm always interested in providing new features.
As for your use case, you might want to have a look at mcipc's own rconclt (which, however uses RCON, not Query), and especially at rconclt <server> in-use and rconclt <server> idle-shutdown.

from mcipc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.