0

I wrote a bot for TeamSpeak 3 that runs over ServerQuery (a telnet interface). But the bot keeps responding later and later, in the beginning it takes like 0.1 sec, after like 1 minute the bot takes about 10 seconds to respond, and using commands makes it even faster.

Any idea why?

So basically the telnet interface sends data from the TS3 Server to my python script, the ts3 module recieves and processes the data, then the script will make a decision of what the action will be.

As modules I am using MySQLdb and ts3(https://github.com/benediktschmitt/py-ts3)

My sourcecode is here: https://pastebin.com/cJuyB9ZH

Another script, which just takes all clients and pushes them into a database every 5 min, runs multiple days without any issues.

I checked the code multiple times now and even deleted variables right after they have been used, but it still has the same issue.

My guess would be that is sortof clogges the RAM, so I looked through the code multiple times, but couldn't find out why or where.

Sidenote: I know I sometimes call a commit() when its totally not necessary, but I don't know if that might cause problems, but I dont see how.

Short(er) version of my code:

import ts3
import MySQLdb
# Some other imports like time and threading and such

## Connect to TS3
tsConn = ts3.query.TS3Connection(tsAddr, tsPort)
try:
    tsConn.login(client_login_name=tsUser, client_login_password=tsPass)
    tsConn.use(sid=tsSID, virtual=True)
    print(" ==>> CONNECTED TO TS3 SERVER: " + tsAddr)
except ts3.query.TS3QueryError as e:
    print("Login to TS Server failed! Aborting...")
    exit(1)

## Connect to mySQL
try:
    qConn = MySQLdb.connect(host=qHost, user=qUser, passwd=qPass, db=qDB)
    qServer = qConn.cursor()
    print(" ==>> CONNECTED TO mySQL SERVER: " + qHost)
except OperationalError:
    print("Cannot connect to mySQL Database! Aborting...")
    exit(1)

running = True
while running:
    tsConn.send_keepalive()
    qServer.execute("SELECT 1") # keepalive

    try:
        e = tsConn.wait_for_event(timeout=1)

    except TS3TimeoutError:
        pass

    else:
        try:
            # <some command processing here>
        except KeyError:
        try:
            if event[0]["reasonid"] == "0":
                tsConn.sendtextmessage(targetmode=1, target=event[0]["clid"], msg=greetingmsg.format(event[0]["client_nickname"]))
        except:
            pass
JH_WK
  • 71
  • 1
  • 9
  • Pleas include the relevant code parts in your post. If you reduce the amount of code to a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) that would be nice. – rollstuhlfahrer Feb 04 '18 at 21:57
  • Kinda hard, since I have absolutely no idea where it happens. But I can try to find where it not happens – JH_WK Feb 04 '18 at 21:58
  • Looked at the code, but found nothing. Have you checked memory consumption and cpu utilization? – rollstuhlfahrer Feb 04 '18 at 22:06
  • CPU is at almost 0, but appears to be going up by about 0.1% every time I call a command, or every 10 seconds. But after a time it evens out at around 6.8%. Memory is constant at 1.2%... All data from a RPi 3 B – JH_WK Feb 04 '18 at 22:06
  • So after looking at it, my guess is that something is wrong with the loop... Something is just draining the CPU in that while loop. – JH_WK Feb 05 '18 at 05:26

0 Answers0