I'm trying to build an API using Flask. If the endpoint /register/string:token is retrieved and the token is in a defined list, the function register_user(username) should be executed. This function sends a command to another server using the rcon protocol. However, as soon as the function is called, I get the following error: ValueError: signal only works in main thread of the main interpreter. How can I fix the problem? Or, how can I run the function in the main thread?
My code looks something like this (Note: The script runs fine on Windows):
from flask import Flask, send_file
from mcrcon import MCRcon as rcon
import configparser
api = Flask(__name__)
# @api.route('/register/<string:token>', methods=['GET'])
def register(token):
cache = utils.load_cache()
if token in list(cache['register_tokens'].keys()):
utils.register_user(username=cache['register_tokens'][token]) # problem!
del cache['register_tokens'][token]
utils.update_cache(obj=cache)
return {'status':'registration successful'}
else:
return {'status':'registration failed'}
class utils:
@staticmethod
def load_cache(file='../cache.pkl'):
...
@staticmethod
def update_cache(obj, file='../cache.pkl'):
...
@staticmethod
def clear_cache():
...
@staticmethod
def register_user(username):
with rcon(config['global']['server_ip'], config['global']['server_rcon_secret']) as console:
r = console.command(...)
console.disconnect()
if __name__ == '__main__':
config = configparser.ConfigParser()
config.read('../config.conf')
if bool(config['discordbot.py']['enable'])==True:
api.add_url_rule('/register/<string:token>', 'register', register)
api.run(host=config['global']['server_ip'], debug=False, use_reloader=False)