Commit 829b4b79 authored by Jerico Moeyersons 's avatar Jerico Moeyersons 🏘
Browse files

Backup 22_01

parent 4abf3776
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/python-3/.devcontainer/base.Dockerfile
# [Choice] Python version: 3, 3.9, 3.8, 3.7, 3.6
ARG VARIANT="3"
FROM mcr.microsoft.com/vscode/devcontainers/python:0-${VARIANT}
# [Option] Install Node.js
ARG INSTALL_NODE="true"
ARG NODE_VERSION="lts/*"
RUN if [ "${INSTALL_NODE}" = "true" ]; then su vscode -c "umask 0002 && . /usr/local/share/nvm/nvm.sh && nvm install ${NODE_VERSION} 2>&1"; fi
# [Optional] If your pip requirements rarely change, uncomment this section to add them to the image.
# COPY requirements.txt /tmp/pip-tmp/
# RUN pip3 --disable-pip-version-check --no-cache-dir install -r /tmp/pip-tmp/requirements.txt \
# && rm -rf /tmp/pip-tmp
# [Optional] Uncomment this section to install additional OS packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
# [Optional] Uncomment this line to install global node packages.
# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
\ No newline at end of file
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.155.1/containers/python-3
{
"name": "Python 3",
"build": {
"dockerfile": "Dockerfile",
"context": "..",
"args": {
// Update 'VARIANT' to pick a Python version: 3, 3.6, 3.7, 3.8, 3.9
"VARIANT": "3.9",
// Options
"INSTALL_NODE": "false",
"NODE_VERSION": "lts/*"
}
},
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"python.pythonPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
"python.linting.mypyPath": "/usr/local/py-utils/bin/mypy",
"python.linting.pycodestylePath": "/usr/local/py-utils/bin/pycodestyle",
"python.linting.pydocstylePath": "/usr/local/py-utils/bin/pydocstyle",
"python.linting.pylintPath": "/usr/local/py-utils/bin/pylint"
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python"
],
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode"
}
{
"python.linting.enabled": true
}
\ No newline at end of file
__author__ = 'Jerico Moeyersons'
import logging
import os
import sys
import socket
from kernel import Kernel
from flask import Flask, request, jsonify
app = Flask(__name__)
logging.basicConfig(filename='api.log', level=logging.WARNING)
logging.captureWarnings(True)
api_log = logging.getLogger()
api_log.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
api_log.addHandler(ch)
logging.info('API Service started on host ' + socket.gethostname())
kernel = Kernel()
\ No newline at end of file
__author__ = 'Jerico Moeyersons'
import http
import logging
import sys
import os
import requests
import concurrent.futures
WORKER_URL = 'http://worker-'
class Kernel(object):
def __init__(self):
logging.info('Initializing API Kernel...')
self.workers = os.getenv('WORKERS', 0)
self.next_worker = 0
if self.workers == 0:
logging.error('No workers defined')
sys.exit(1)
else:
self.next_worker = 1
self.active = False
def start_worker(self, worker=None):
if worker is None:
url = WORKER_URL + self.next_worker + "/start"
self.__set_next_worker()
else:
# TODO: check if worker exists
url = WORKER_URL + worker + "/start"
logging.info('Starting working ' + str(self.next_worker))
try:
r = requests.get(url)
status_code = r.status_code
if status_code != 200:
logging.warning("Worker returned a non 200 HTTP status code")
return False
else:
logging.info("Worker finished successfully")
except requests.ConnectionError:
logging.error('Connection error in function start worker')
return False
except requests.Timeout:
logging.error('Timeout error in function start worker')
return False
return True
def multiple_start(self, number):
if not self.active:
if number < self.workers:
with concurrent.futures.ThreadPoolExecutor(max_workers=number) as executor:
executor.submit(self.start_worker, self)
'''for i in range(1, number+1):
thread = Thread(target = start_worker, args = (i, ))
threads.append(thread)
thread.start()
for index, thread in enumerate(threads):
logging.info("Main : before joining thread %d.", index)
thread.join()
logging.info("Main : thread %d done", index)'''
else:
with concurrent.futures.ThreadPoolExecutor(max_workers=self.workers) as executor:
executor.submit(self.start_worker, self)
return "All workers started and finished..."
else:
return "Another job is running, please try again later..."
def __set_next_worker(self):
if self.next_worker == self.worker:
self.next_worker = 1
else:
self.next_worker += 1
flask
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment