<commit_before>import thread
import json
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
from game import Game


def client_thread(game, conn, data):
    player = game.add_player(conn, data)

    while True:
        answer_data = game.wait_for_answer(player)
        if answer_data:
            conn.sendMessage(answer_data)
        request = conn.wait()
        print request

    # Thread loop ended
    conn.sendClose()


class CartetsServer(WebSocket):
    def handleMessage(self):
        if not self.data:
            return {}
        try:
            data = json.loads(self.data.decode('utf-8'))
            value = data['action']
        except Exception:
            data = {}
            value = ''

        if value == 'init':
            thread.start_new_thread(client_thread, (game, self, data))

        # self.sendMessage(str(self.data))
        return data

    def handleConnected(self):
        print self.address, 'connected'

    def handleClose(self):
        print self.address, 'closed'

    def wait(self):
        while True:
            data = self.handleMessage()
            if data:
                break
        return data


game = Game()
server = SimpleWebSocketServer('', 8080, CartetsServer)
server.serveforever()
<commit_msg>Fix high cpu usage through sleep<commit_after>import thread
import json
import time
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
from game import Game


def client_thread(game, conn, data):
    player = game.add_player(conn, data)

    while True:
        answer_data = game.wait_for_answer(player)
        if answer_data:
            conn.sendMessage(answer_data)
        request = conn.wait()
        print request

    # Thread loop ended
    conn.sendClose()


class CartetsServer(WebSocket):
    def handleMessage(self):
        if not self.data:
            return {}
        try:
            data = json.loads(self.data.decode('utf-8'))
            value = data['action']
        except Exception:
            data = {}
            value = ''

        if value == 'init':
            thread.start_new_thread(client_thread, (game, self, data))

        # self.sendMessage(str(self.data))
        return data

    def handleConnected(self):
        print self.address, 'connected'

    def handleClose(self):
        print self.address, 'closed'

    def wait(self):
        while True:
            data = self.handleMessage()
            if data:
                break
            else:
                time.sleep(0.5)
        return data


game = Game()
server = SimpleWebSocketServer('', 8080, CartetsServer)
server.serveforever()

{'fraction_non_alphanumeric': 0.09352517985611511, 'fraction_numerical': 0.004543733434305188, 'mean_word_length': 2.115566037735849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7120234', 'n_tokens_mistral': 742, 'n_tokens_neox': 712, 'n_words': 189}