Upload 7 files
Browse filesFiles changed (7)
- DockerfileAdded+38
- app.pyAdded+251
- build.pyAdded+27
- center.pyAdded+1392
- concurrent_test.pyAdded+32
- config.yamlAdded+44
- requirements.txtAdded+24
1234567891011121314151617181920212223242526272829303132333435363738# 第一阶段:构建阶段
FROM python:3.10-slim AS builder
# 设置工作目录
WORKDIR /app
# 复制依赖文件
COPY requirements.txt .
# 安装构建依赖
RUN apt-get update && apt-get install -y \
build-essential \
libssl-dev \
libffi-dev \
python3-dev \
&& apt-get clean \
&& pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir -r requirements.txt
# 第二阶段:运行阶段
FROM python:3.10-slim
# 设置工作目录
WORKDIR /app
# 从构建阶段复制必要的文件
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin
COPY . .
# 设置环境变量
ENV PYTHONUNBUFFERED=1
# 暴露 Flask 端口
EXPOSE 5000
# 运行 Flask 应用
CMD ["gunicorn", "-b", "0.0.0.0:5000", "app:app"]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251from flask import Flask, request, jsonify, Response
import subprocess
import sys
import threading
import time
import requests
from functools import wraps
import urllib.parse
import re
import json
import base64
import os
import glob
app = Flask(__name__)
# 从环境变量中读取配置,如果未设置则使用默认值
OUTPUT_BASE64 = os.getenv('OUTPUT_BASE64', 'True').lower() == 'true'
AUTH_TOKEN = os.getenv('AUTH_TOKEN', '')
# HOST = os.getenv('HOST', '127.0.0.1')
HOST = os.getenv('HOST', '0.0.0.0')
PORT = int(os.getenv('PORT', 5000))
TIMEOUT = int(os.getenv('TIMEOUT', 30))
REMOVE_PYTHON_CODE_BLOCK = os.getenv('REMOVE_PYTHON_CODE_BLOCK', 'True').lower() == 'true'
# 配置变量,决定是否启用单元测试,默认启用
ENABLE_UNIT_TESTS = False
def auth_required(f):
@wraps(f)
def decorated(*args, **kwargs):
if AUTH_TOKEN:
auth_header = request.headers.get('Authorization')
if not auth_header or not auth_header.startswith('Bearer '):
return jsonify({'error': 'Authorization header is missing or invalid'}), 401
token = auth_header.split('Bearer ')[1]
if token != AUTH_TOKEN:
return jsonify({'error': 'Invalid token'}), 401
return f(*args, **kwargs)
return decorated
def extract_code(code):
if REMOVE_PYTHON_CODE_BLOCK:
match = re.search(r'```python\s*(.*?)\s*```', code, re.DOTALL)
if match:
return match.group(1)
return code
def run_code(language, code, variables, timeout):
try:
# 根据语言类型选择解释器
if language == 'python':
# 提取代码中的有效部分
code = extract_code(code)
# 将变量注入到代码环境中
var_definitions = '\n'.join(f"{key} = {value}" for key, value in variables.items())
code_with_vars = f"{var_definitions}\n{code}"
# 使用subprocess运行代码
result = subprocess.run([sys.executable, '-c', code_with_vars], capture_output=True, text=True, timeout=timeout)
return result.stdout, result.stderr
else:
return None, "Unsupported language"
except subprocess.TimeoutExpired:
return None, "Code execution timed out"
except Exception as e:
return None, str(e)
def convert_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
return encoded_string
def unescape_string(s):
return s.encode('utf-8').decode('unicode_escape')
@app.route('/runcode', methods=['POST', 'GET'])
@auth_required
def run_code_endpoint():
if request.method == 'POST':
data = request.get_json()
if not data:
return jsonify({'error': 'Invalid JSON'}), 400
language = data.get('languageType', 'python')
variables = data.get('variables', {})
code = data.get('code', '')
elif request.method == 'GET':
query_string = request.query_string.decode('utf-8')
print("Query String:",query_string)
# 检查query_string中是否有"+"
if '+' in query_string:
# query_string的+替换为空格
query_string = re.sub(r'\+', ' ', query_string)
print("Query String:",query_string)
# 如果没有+,则直接输出query_string
else:
print("Query String:",query_string)
# 使用正则表达式提取参数
language_match = re.search(r'languageType=("?)([^"&]+)\1', query_string)
variables_match = re.search(r'variables=("?)([^"&]+)\1', query_string)
code_match = re.search(r'code=("?)([^"&]+)\1', query_string)
print("code_match:",code_match)
if not language_match or not variables_match or not code_match:
return jsonify({'error': 'Invalid parameters'}), 400
language = urllib.parse.unquote(language_match.group(2))
variables = urllib.parse.unquote(variables_match.group(2))
code = urllib.parse.unquote(code_match.group(2))
print("code:",code)
# 处理转义字符
language = unescape_string(language)
variables = unescape_string(variables)
code = unescape_string(code)
print("code:",code)
# # 使用正则表达式将 + 替换为空格
# code = re.sub(r'\+', ' ', code)
# print("code:",code)
try:
variables = json.loads(variables)
except json.JSONDecodeError:
return jsonify({'error': 'Invalid variables format'}), 400
# 打印解析后的JSON参数
print("Parsed JSON Parameters:")
print(json.dumps({
'languageType': language,
'variables': variables,
'code': code
}, indent=4))
else:
return jsonify({'error': 'Unsupported HTTP method'}), 405
# 运行代码
stdout, stderr = run_code(language, code, variables, TIMEOUT)
if stderr:
return jsonify({'error': stderr}), 400
else:
# 查找当前目录中的所有图片文件
image_files = glob.glob('*.png') + glob.glob('*.jpg') + glob.glob('*.jpeg') + glob.glob('*.gif')
if image_files:
image_data = {}
for image_path in image_files:
encoded_image = convert_image_to_base64(image_path)
image_data[image_path] = encoded_image
os.remove(image_path) # 删除生成的图片文件
if not OUTPUT_BASE64:
# 如果 OUTPUT_BASE64 不是 True,则输出原始图片
html_content = ''.join([f'<img src="{image_path}" alt="{image_path}" />' for image_path in image_data.keys()])
return Response(html_content, mimetype='text/html')
else:
return jsonify({'output': stdout, 'images': image_data}), 200
else:
return jsonify({'output': stdout}), 200
def run_test_case(test_case):
url = f'http://{HOST}:{PORT}/runcode'
if test_case['method'] == 'GET':
params = {
'languageType': test_case['language'],
'variables': test_case['variables'],
'code': test_case['code']
}
headers = {'Authorization': f'Bearer {AUTH_TOKEN}'} if AUTH_TOKEN else {}
print("Request Params:")
print(params)
response = requests.get(url, params=params, headers=headers)
elif test_case['method'] == 'POST':
data = {
'languageType': test_case['language'],
'variables': test_case['variables'],
'code': test_case['code']
}
headers = {'Authorization': f'Bearer {AUTH_TOKEN}'} if AUTH_TOKEN else {}
print("Request Data:")
print(data)
response = requests.post(url, json=data, headers=headers)
else:
raise ValueError("Unsupported HTTP method in test case")
# 打印返回JSON
print("Response JSON:")
print(response.json())
def test_run_code_endpoint():
test_cases = [
{
'method': 'GET',
'language': 'python',
'variables': str({}),
'code': "print('Hello, World!')"
},
{
'method': 'GET',
'language': '"python"',
'variables': str({}),
'code': '"print(\'Hello, World!\')"'
},
{
'method': 'GET',
'language': 'python',
'variables': str({}),
'code': "print('Hello%20World')"
},
{
'method': 'POST',
'language': 'python',
'variables': {},
'code': "print(5 ** 11)"
},
{
'method': 'POST',
'language': 'python',
'variables': {'m': 7, 'n': 4},
'code': "print(m * n)"
},
{
'method': 'POST',
'language': 'python',
'variables': {},
'code': "```python\nprint('Hello from code block!')\n```"
}
]
for test_case in test_cases:
run_test_case(test_case)
if __name__ == '__main__':
# 启动Flask应用
threading.Thread(target=app.run, kwargs={'host': HOST, 'port': PORT, 'debug': False}).start()
# 等待Flask应用启动
time.sleep(1)
# 运行单元测试
if ENABLE_UNIT_TESTS:
test_run_code_endpoint()
print("All tests executed!")123456789101112131415161718192021222324252627import yaml
import subprocess
# 读取配置文件,指定编码为 utf-8
with open('config.yaml', 'r', encoding='utf-8') as file:
config = yaml.safe_load(file)
DEPENDENCIES = config['dependencies']
INTERPRETER_IMAGE = config['interpreter_image']
# 动态生成 requirements.txt
def generate_requirements(dependencies):
with open('requirements.txt', 'w', encoding='utf-8') as f:
for dep in dependencies:
f.write(f"{dep}\n")
generate_requirements(DEPENDENCIES)
print("Generated requirements.txt with specified dependencies.")
# 构建 Docker 镜像
def build_docker_image(image_name):
build_command = ["docker", "build", "-t", image_name, "."]
subprocess.run(build_command, check=True)
print(f"Built Docker image {image_name}")
build_docker_image(INTERPRETER_IMAGE)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392# # # # import yaml
# # # # import subprocess
# # # # import uuid
# # # # from flask import Flask, request, jsonify
# # # # from queue import Queue
# # # # import requests
# # # # import threading
# # # # import time
# # # # import os
# # # # import atexit
# # # # import base64
# # # # import signal
# # # # from sqlalchemy import create_engine, Column, String, LargeBinary, Integer
# # # # from sqlalchemy.orm import declarative_base, sessionmaker
# # # # from sqlalchemy.exc import OperationalError, IntegrityError
# # # # import psycopg2
# # # # app = Flask(__name__)
# # # # # 读取配置文件,指定编码为 utf-8
# # # # with open('config.yaml', 'r', encoding='utf-8') as file:
# # # # config = yaml.safe_load(file)
# # # # DOMAIN = config['domain']
# # # # INTERPRETER_IMAGE = config['interpreter_image']
# # # # PORT_START = config['interpreter_port_range']['start']
# # # # PORT_END = config['interpreter_port_range']['end']
# # # # DEPENDENCIES = config['dependencies']
# # # # RESOURCE_LIMITS = config.get('resource_limits', {})
# # # # MEMORY_LIMIT = RESOURCE_LIMITS.get('memory')
# # # # CPU_LIMIT = RESOURCE_LIMITS.get('cpus')
# # # # POSTGRES = config['postgres']
# # # # TIMEOUT = config.get('timeout', 30) # 默认超时时间为30秒
# # # # SCHEDULER_PORT = config['scheduler_port'] # 新增调度中心端口
# # # # # 容器管理
# # # # containers = []
# # # # ports = list(range(PORT_START, PORT_END + 1))
# # # # lock = threading.Lock()
# # # # request_queue = Queue()
# # # # result_dict = {} # 用于存储结果的字典
# # # # current_container_index = 0
# # # # # 检查 PostgreSQL 容器是否存在
# # # # POSTGRES_CONTAINER_NAME = "postgres_code_interpreter"
# # # # POSTGRES_DATA_DIR = os.path.join(os.getcwd(), 'pgdata')
# # # # def is_postgres_container_running():
# # # # result = subprocess.run(["docker", "ps", "-a", "--filter", f"name={POSTGRES_CONTAINER_NAME}", "--format", "{{.Names}}"], capture_output=True, text=True)
# # # # return POSTGRES_CONTAINER_NAME in result.stdout.strip().split('\n')
# # # # def wait_for_postgres_ready():
# # # # retry_attempts = 10
# # # # retry_delay = 5 # seconds
# # # # for attempt in range(retry_attempts):
# # # # try:
# # # # conn = psycopg2.connect(
# # # # dbname=POSTGRES['db'],
# # # # user=POSTGRES['user'],
# # # # password=POSTGRES['password'],
# # # # host=POSTGRES['host'],
# # # # port=POSTGRES['port']
# # # # )
# # # # conn.close()
# # # # print("PostgreSQL is ready")
# # # # return True
# # # # except psycopg2.OperationalError as e:
# # # # print(f"Waiting for PostgreSQL to be ready: {e}")
# # # # time.sleep(retry_delay)
# # # # return False
# # # # if not is_postgres_container_running():
# # # # if not os.path.exists(POSTGRES_DATA_DIR):
# # # # os.makedirs(POSTGRES_DATA_DIR)
# # # # subprocess.run([
# # # # "docker", "run", "--name", POSTGRES_CONTAINER_NAME, "-d",
# # # # "-e", f"POSTGRES_USER={POSTGRES['user']}",
# # # # "-e", f"POSTGRES_PASSWORD={POSTGRES['password']}",
# # # # "-e", f"POSTGRES_DB={POSTGRES['db']}",
# # # # "-v", f"{POSTGRES_DATA_DIR}:/var/lib/postgresql/data",
# # # # "-p", f"{POSTGRES['port']}:5432",
# # # # "postgres"
# # # # ], check=True)
# # # # if not wait_for_postgres_ready():
# # # # print("Failed to connect to PostgreSQL after multiple attempts.")
# # # # exit(1)
# # # # # PostgreSQL 配置
# # # # DATABASE_URL = f"postgresql://{POSTGRES['user']}:{POSTGRES['password']}@{POSTGRES['host']}:{POSTGRES['port']}/{POSTGRES['db']}"
# # # # engine = None
# # # # Base = declarative_base()
# # # # Session = None
# # # # session = None
# # # # def init_db():
# # # # global engine, Session, session
# # # # retry_attempts = 5
# # # # retry_delay = 5 # seconds
# # # # for attempt in range(retry_attempts):
# # # # try:
# # # # engine = create_engine(DATABASE_URL)
# # # # Session = sessionmaker(bind=engine)
# # # # session = Session()
# # # # Base.metadata.create_all(engine) # 确保数据库模式已创建
# # # # print("Database connection successful")
# # # # break
# # # # except OperationalError as e:
# # # # print(f"Database connection failed: {e}")
# # # # if attempt < retry_attempts - 1:
# # # # print(f"Retrying in {retry_delay} seconds...")
# # # # time.sleep(retry_delay)
# # # # else:
# # # # print("Failed to connect to the database after multiple attempts.")
# # # # raise
# # # # class Image(Base):
# # # # __tablename__ = 'images'
# # # # id = Column(Integer, primary_key=True, autoincrement=True)
# # # # filename = Column(String, unique=True, nullable=False)
# # # # data = Column(LargeBinary, nullable=False)
# # # # init_db()
# # # # # 启动代码解释器容器
# # # # def start_container(port):
# # # # container_name = f"code_interpreter_docker_{uuid.uuid4()}"
# # # # run_command = [
# # # # "docker", "run", "--name", container_name, "-d", "-p", f"{port}:5000"
# # # # ]
# # # # if MEMORY_LIMIT:
# # # # run_command.extend(["--memory", MEMORY_LIMIT])
# # # # if CPU_LIMIT:
# # # # run_command.extend(["--cpus", CPU_LIMIT])
# # # # run_command.append(INTERPRETER_IMAGE)
# # # # subprocess.run(run_command, check=True)
# # # # print(f"Started container {container_name} on port {port}")
# # # # return container_name, port
# # # # def stop_container(container_name):
# # # # subprocess.run(["docker", "stop", container_name])
# # # # subprocess.run(["docker", "rm", container_name])
# # # # print(f"Stopped and removed container {container_name}")
# # # # def start_containers():
# # # # for port in ports:
# # # # container_name, port = start_container(port)
# # # # containers.append((container_name, port))
# # # # def stop_containers():
# # # # for container_name, _ in containers:
# # # # stop_container(container_name)
# # # # @app.route('/runcode', methods=['POST', 'GET'])
# # # # def run_code():
# # # # if request.method == 'POST':
# # # # data = request.get_json()
# # # # if not data:
# # # # return jsonify({'error': 'Invalid JSON'}), 400
# # # # elif request.method == 'GET':
# # # # query_string = request.query_string.decode('utf-8')
# # # # data = {'query_string': query_string}
# # # # else:
# # # # return jsonify({'error': 'Invalid request method'}), 405
# # # # request_id = str(uuid.uuid4()) # 生成一个唯一的请求ID
# # # # request_queue.put((request_id, data))
# # # # start_time = time.time()
# # # # while time.time() - start_time < 10: # 最多等待10秒
# # # # if request_id in result_dict:
# # # # output = result_dict.pop(request_id)
# # # # # 处理返回结果,替换 base64 数据为链接
# # # # if 'images' in output:
# # # # try:
# # # # output = process_images(output)
# # # # except Exception as e:
# # # # return jsonify({'error': str(e)}), 500
# # # # return jsonify(output), 200
# # # # time.sleep(0.1)
# # # # return jsonify({'error': 'Request timed out'}), 504
# # # # def process_images(output):
# # # # images = output['images']
# # # # for filename, base64_data in images.items():
# # # # image_data = base64.b64decode(base64_data)
# # # # unique_filename = f"{uuid.uuid4()}_{filename}"
# # # # image_record = Image(filename=unique_filename, data=image_data)
# # # # try:
# # # # session.add(image_record)
# # # # session.commit()
# # # # except IntegrityError as e:
# # # # session.rollback()
# # # # print(f"Database error: {e}")
# # # # continue
# # # # if DOMAIN:
# # # # images[filename] = f"https://{DOMAIN}/image/{unique_filename}"
# # # # else:
# # # # images[filename] = f"http://127.0.0.1:{SCHEDULER_PORT}/image/{unique_filename}"
# # # # return output
# # # # @app.route('/image/<filename>', methods=['GET'])
# # # # def serve_image(filename):
# # # # image_record = session.query(Image).filter_by(filename=filename).first()
# # # # if image_record:
# # # # return image_record.data, 200, {'Content-Type': 'image/png'}
# # # # return jsonify({'error': 'Image not found'}), 404
# # # # def handle_requests():
# # # # global current_container_index
# # # # while True:
# # # # request_id, data = request_queue.get()
# # # # if data is None:
# # # # break
# # # # with lock:
# # # # container_name, port = containers[current_container_index]
# # # # current_container_index = (current_container_index + 1) % len(containers)
# # # # try:
# # # # if 'query_string' in data:
# # # # # GET 请求处理
# # # # response = requests.get(f"http://localhost:{port}/runcode?{data['query_string']}", timeout=TIMEOUT)
# # # # else:
# # # # # POST 请求处理
# # # # response = requests.post(f"http://localhost:{port}/runcode", json=data, timeout=TIMEOUT)
# # # # output = response.json()
# # # # except requests.exceptions.Timeout:
# # # # output = {'error': 'Code execution timed out'}
# # # # reset_container(container_name, port)
# # # # except Exception as e:
# # # # output = {'error': str(e)}
# # # # result_dict[request_id] = output # 将结果放入结果字典
# # # # request_queue.task_done()
# # # # print(f"Finished processing request {request_id}: {output}")
# # # # def reset_container(container_name, port):
# # # # print(f"Resetting container {container_name}")
# # # # stop_container(container_name)
# # # # new_container_name, _ = start_container(port)
# # # # with lock:
# # # # for i, (name, p) in enumerate(containers):
# # # # if name == container_name:
# # # # containers[i] = (new_container_name, port)
# # # # break
# # # # def signal_handler(signal, frame):
# # # # print('Stopping containers and exiting program...')
# # # # stop_containers()
# # # # if session:
# # # # session.close()
# # # # engine.dispose()
# # # # os._exit(0)
# # # # signal.signal(signal.SIGINT, signal_handler)
# # # # worker_thread = threading.Thread(target=handle_requests)
# # # # worker_thread.start()
# # # # start_containers()
# # # # atexit.register(stop_containers)
# # # # if __name__ == '__main__':
# # # # app.run(port=SCHEDULER_PORT, threaded=True) # 使用调度中心端口
# # # import yaml
# # # import subprocess
# # # import uuid
# # # from flask import Flask, request, jsonify
# # # from queue import Queue
# # # import requests
# # # import threading
# # # import time
# # # import os
# # # import atexit
# # # import base64
# # # import signal
# # # from sqlalchemy import create_engine, Column, String, LargeBinary, Integer
# # # from sqlalchemy.orm import declarative_base, sessionmaker
# # # from sqlalchemy.exc import OperationalError, IntegrityError
# # # import psycopg2
# # # app = Flask(__name__)
# # # # 读取配置文件,指定编码为 utf-8
# # # with open('config.yaml', 'r', encoding='utf-8') as file:
# # # config = yaml.safe_load(file)
# # # DOMAIN = config['domain']
# # # INTERPRETER_IMAGE = config['interpreter_image']
# # # PORT_START = config['interpreter_port_range']['start']
# # # PORT_END = config['interpreter_port_range']['end']
# # # DEPENDENCIES = config['dependencies']
# # # RESOURCE_LIMITS = config.get('resource_limits', {})
# # # MEMORY_LIMIT = RESOURCE_LIMITS.get('memory')
# # # CPU_LIMIT = RESOURCE_LIMITS.get('cpus')
# # # POSTGRES = config['postgres']
# # # TIMEOUT = config.get('timeout', 30) # 默认超时时间为30秒
# # # SCHEDULER_PORT = config['scheduler_port'] # 新增调度中心端口
# # # # 容器管理
# # # containers = []
# # # ports = list(range(PORT_START, PORT_END + 1))
# # # lock = threading.Lock()
# # # request_queue = Queue()
# # # result_dict = {} # 用于存储结果的字典
# # # current_container_index = 0
# # # # 检查 PostgreSQL 容器是否存在
# # # POSTGRES_CONTAINER_NAME = "postgres_code_interpreter"
# # # POSTGRES_DATA_DIR = os.path.join(os.getcwd(), 'pgdata')
# # # def is_postgres_container_running():
# # # result = subprocess.run(["docker", "ps", "-a", "--filter", f"name={POSTGRES_CONTAINER_NAME}", "--format", "{{.Names}}"], capture_output=True, text=True)
# # # return POSTGRES_CONTAINER_NAME in result.stdout.strip().split('\n')
# # # def wait_for_postgres_ready():
# # # retry_attempts = 10
# # # retry_delay = 5 # seconds
# # # for attempt in range(retry_attempts):
# # # try:
# # # conn = psycopg2.connect(
# # # dbname=POSTGRES['db'],
# # # user=POSTGRES['user'],
# # # password=POSTGRES['password'],
# # # host=POSTGRES['host'],
# # # port=POSTGRES['port']
# # # )
# # # conn.close()
# # # print("PostgreSQL is ready")
# # # return True
# # # except psycopg2.OperationalError as e:
# # # print(f"Waiting for PostgreSQL to be ready: {e}")
# # # time.sleep(retry_delay)
# # # return False
# # # if not is_postgres_container_running():
# # # if not os.path.exists(POSTGRES_DATA_DIR):
# # # os.makedirs(POSTGRES_DATA_DIR)
# # # subprocess.run([
# # # "docker", "run", "--name", POSTGRES_CONTAINER_NAME, "-d",
# # # "-e", f"POSTGRES_USER={POSTGRES['user']}",
# # # "-e", f"POSTGRES_PASSWORD={POSTGRES['password']}",
# # # "-e", f"POSTGRES_DB={POSTGRES['db']}",
# # # "-v", f"{POSTGRES_DATA_DIR}:/var/lib/postgresql/data",
# # # "-p", f"{POSTGRES['port']}:5432",
# # # "postgres"
# # # ], check=True)
# # # if not wait_for_postgres_ready():
# # # print("Failed to connect to PostgreSQL after multiple attempts.")
# # # exit(1)
# # # # PostgreSQL 配置
# # # DATABASE_URL = f"postgresql://{POSTGRES['user']}:{POSTGRES['password']}@{POSTGRES['host']}:{POSTGRES['port']}/{POSTGRES['db']}"
# # # engine = None
# # # Base = declarative_base()
# # # Session = None
# # # session = None
# # # def init_db():
# # # global engine, Session, session
# # # retry_attempts = 5
# # # retry_delay = 5 # seconds
# # # for attempt in range(retry_attempts):
# # # try:
# # # engine = create_engine(DATABASE_URL)
# # # Session = sessionmaker(bind=engine)
# # # session = Session()
# # # Base.metadata.create_all(engine) # 确保数据库模式已创建
# # # print("Database connection successful")
# # # break
# # # except OperationalError as e:
# # # print(f"Database connection failed: {e}")
# # # if attempt < retry_attempts - 1:
# # # print(f"Retrying in {retry_delay} seconds...")
# # # time.sleep(retry_delay)
# # # else:
# # # print("Failed to connect to the database after multiple attempts.")
# # # raise
# # # class Image(Base):
# # # __tablename__ = 'images'
# # # id = Column(Integer, primary_key=True, autoincrement=True)
# # # filename = Column(String, unique=True, nullable=False)
# # # data = Column(LargeBinary, nullable=False)
# # # init_db()
# # # # 启动代码解释器容器
# # # def start_container(port):
# # # container_name = f"code_interpreter_docker_{uuid.uuid4()}"
# # # run_command = [
# # # "docker", "run", "--name", container_name, "-d", "-p", f"{port}:5000"
# # # ]
# # # if MEMORY_LIMIT:
# # # run_command.extend(["--memory", MEMORY_LIMIT])
# # # if CPU_LIMIT:
# # # run_command.extend(["--cpus", CPU_LIMIT])
# # # run_command.append(INTERPRETER_IMAGE)
# # # subprocess.run(run_command, check=True)
# # # print(f"Started container {container_name} on port {port}")
# # # return container_name, port
# # # def stop_container(container_name):
# # # subprocess.run(["docker", "stop", container_name])
# # # subprocess.run(["docker", "rm", container_name])
# # # print(f"Stopped and removed container {container_name}")
# # # def start_containers():
# # # for port in ports:
# # # container_name, port = start_container(port)
# # # containers.append((container_name, port))
# # # def stop_containers():
# # # for container_name, _ in containers:
# # # stop_container(container_name)
# # # @app.route('/runcode', methods=['POST', 'GET'])
# # # def run_code():
# # # if request.method == 'POST':
# # # data = request.get_json()
# # # if not data:
# # # return jsonify({'error': 'Invalid JSON'}), 400
# # # elif request.method == 'GET':
# # # query_string = request.query_string.decode('utf-8')
# # # data = {'query_string': query_string}
# # # else:
# # # return jsonify({'error': 'Invalid request method'}), 405
# # # request_id = str(uuid.uuid4()) # 生成一个唯一的请求ID
# # # request_queue.put((request_id, data))
# # # start_time = time.time()
# # # while time.time() - start_time < 10: # 最多等待10秒
# # # if request_id in result_dict:
# # # output = result_dict.pop(request_id)
# # # # 处理返回结果,替换 base64 数据为链接
# # # if 'images' in output:
# # # try:
# # # output = process_images(output)
# # # except Exception as e:
# # # return jsonify({'error': str(e)}), 500
# # # return jsonify(output), 200
# # # time.sleep(0.1)
# # # return jsonify({'error': 'Request timed out'}), 504
# # # def process_images(output):
# # # images = output['images']
# # # for filename, base64_data in images.items():
# # # image_data = base64.b64decode(base64_data)
# # # unique_filename = f"{uuid.uuid4()}_{filename}"
# # # image_record = Image(filename=unique_filename, data=image_data)
# # # try:
# # # session.add(image_record)
# # # session.commit()
# # # except IntegrityError as e:
# # # session.rollback()
# # # print(f"Database error: {e}")
# # # continue
# # # if DOMAIN:
# # # images[filename] = f"https://{DOMAIN}/image/{unique_filename}"
# # # else:
# # # images[filename] = f"http://127.0.0.1:{SCHEDULER_PORT}/image/{unique_filename}"
# # # return output
# # # @app.route('/image/<filename>', methods=['GET'])
# # # def serve_image(filename):
# # # image_record = session.query(Image).filter_by(filename=filename).first()
# # # if image_record:
# # # return image_record.data, 200, {'Content-Type': 'image/png'}
# # # return jsonify({'error': 'Image not found'}), 404
# # # def handle_requests():
# # # global current_container_index
# # # while True:
# # # request_id, data = request_queue.get()
# # # if data is None:
# # # break
# # # with lock:
# # # container_name, port = containers[current_container_index]
# # # current_container_index = (current_container_index + 1) % len(containers)
# # # try:
# # # if 'query_string' in data:
# # # # GET 请求处理
# # # response = requests.get(f"http://localhost:{port}/runcode?{data['query_string']}", timeout=TIMEOUT)
# # # else:
# # # # POST 请求处理
# # # response = requests.post(f"http://localhost:{port}/runcode", json=data, timeout=TIMEOUT)
# # # output = response.json()
# # # except requests.exceptions.Timeout:
# # # output = {'error': 'Code execution timed out'}
# # # reset_container(container_name, port)
# # # except Exception as e:
# # # output = {'error': str(e)}
# # # result_dict[request_id] = output # 将结果放入结果字典
# # # request_queue.task_done()
# # # print(f"Finished processing request {request_id}: {output}")
# # # # Reset container after each request
# # # reset_container(container_name, port)
# # # def reset_container(container_name, port):
# # # print(f"Resetting container {container_name}")
# # # stop_container(container_name)
# # # new_container_name, _ = start_container(port)
# # # with lock:
# # # for i, (name, p) in enumerate(containers):
# # # if name == container_name:
# # # containers[i] = (new_container_name, port)
# # # break
# # # def signal_handler(signal, frame):
# # # print('Stopping containers and exiting program...')
# # # stop_containers()
# # # if session:
# # # session.close()
# # # engine.dispose()
# # # os._exit(0)
# # # signal.signal(signal.SIGINT, signal_handler)
# # # worker_thread = threading.Thread(target=handle_requests)
# # # worker_thread.start()
# # # start_containers()
# # # atexit.register(stop_containers)
# # # if __name__ == '__main__':
# # # app.run(port=SCHEDULER_PORT, threaded=True) # 使用调度中心端口
# # import yaml
# # import subprocess
# # import uuid
# # from flask import Flask, request, jsonify
# # from queue import Queue
# # import requests
# # import threading
# # import time
# # import os
# # import atexit
# # import base64
# # import signal
# # from sqlalchemy import create_engine, Column, String, LargeBinary, Integer
# # from sqlalchemy.orm import declarative_base, sessionmaker
# # from sqlalchemy.exc import OperationalError, IntegrityError
# # import psycopg2
# # app = Flask(__name__)
# # # 读取配置文件,指定编码为 utf-8
# # with open('config.yaml', 'r', encoding='utf-8') as file:
# # config = yaml.safe_load(file)
# # DOMAIN = config['domain']
# # INTERPRETER_IMAGE = config['interpreter_image']
# # PORT_START = config['interpreter_port_range']['start']
# # PORT_END = config['interpreter_port_range']['end']
# # DEPENDENCIES = config['dependencies']
# # RESOURCE_LIMITS = config.get('resource_limits', {})
# # MEMORY_LIMIT = RESOURCE_LIMITS.get('memory')
# # CPU_LIMIT = RESOURCE_LIMITS.get('cpus')
# # POSTGRES = config['postgres']
# # TIMEOUT = config.get('timeout', 30) # 默认超时时间为30秒
# # TIMEOUT_SECONDS = config.get('timeout_seconds', 60) # 从配置中读取超时时间
# # SCHEDULER_PORT = config['scheduler_port'] # 新增调度中心端口
# # # 容器管理
# # containers = []
# # ports = list(range(PORT_START, PORT_END + 1))
# # lock = threading.Lock()
# # request_queue = Queue()
# # result_dict = {} # 用于存储结果的字典
# # current_container_index = 0
# # semaphore = threading.Semaphore(len(ports)) # 控制并发请求的信号量
# # # 检查 PostgreSQL 容器是否存在
# # POSTGRES_CONTAINER_NAME = "postgres_code_interpreter"
# # POSTGRES_DATA_DIR = os.path.join(os.getcwd(), 'pgdata')
# # def is_postgres_container_running():
# # result = subprocess.run(["docker", "ps", "-a", "--filter", f"name={POSTGRES_CONTAINER_NAME}", "--format", "{{.Names}}"], capture_output=True, text=True)
# # return POSTGRES_CONTAINER_NAME in result.stdout.strip().split('\n')
# # def wait_for_postgres_ready():
# # retry_attempts = 10
# # retry_delay = 5 # seconds
# # for attempt in range(retry_attempts):
# # try:
# # conn = psycopg2.connect(
# # dbname=POSTGRES['db'],
# # user=POSTGRES['user'],
# # password=POSTGRES['password'],
# # host=POSTGRES['host'],
# # port=POSTGRES['port']
# # )
# # conn.close()
# # print("PostgreSQL is ready")
# # return True
# # except psycopg2.OperationalError as e:
# # print(f"Waiting for PostgreSQL to be ready: {e}")
# # time.sleep(retry_delay)
# # return False
# # if not is_postgres_container_running():
# # if not os.path.exists(POSTGRES_DATA_DIR):
# # os.makedirs(POSTGRES_DATA_DIR)
# # subprocess.run([
# # "docker", "run", "--name", POSTGRES_CONTAINER_NAME, "-d",
# # "-e", f"POSTGRES_USER={POSTGRES['user']}",
# # "-e", f"POSTGRES_PASSWORD={POSTGRES['password']}",
# # "-e", f"POSTGRES_DB={POSTGRES['db']}",
# # "-v", f"{POSTGRES_DATA_DIR}:/var/lib/postgresql/data",
# # "-p", f"{POSTGRES['port']}:5432",
# # "postgres"
# # ], check=True)
# # if not wait_for_postgres_ready():
# # print("Failed to connect to PostgreSQL after multiple attempts.")
# # exit(1)
# # # PostgreSQL 配置
# # DATABASE_URL = f"postgresql://{POSTGRES['user']}:{POSTGRES['password']}@{POSTGRES['host']}:{POSTGRES['port']}/{POSTGRES['db']}"
# # engine = None
# # Base = declarative_base()
# # Session = None
# # session = None
# # def init_db():
# # global engine, Session, session
# # retry_attempts = 5
# # retry_delay = 5 # seconds
# # for attempt in range(retry_attempts):
# # try:
# # engine = create_engine(DATABASE_URL)
# # Session = sessionmaker(bind=engine)
# # session = Session()
# # Base.metadata.create_all(engine) # 确保数据库模式已创建
# # print("Database connection successful")
# # break
# # except OperationalError as e:
# # print(f"Database connection failed: {e}")
# # if attempt < retry_attempts - 1:
# # print(f"Retrying in {retry_delay} seconds...")
# # time.sleep(retry_delay)
# # else:
# # print("Failed to connect to the database after multiple attempts.")
# # raise
# # class Image(Base):
# # __tablename__ = 'images'
# # id = Column(Integer, primary_key=True, autoincrement=True)
# # filename = Column(String, unique=True, nullable=False)
# # data = Column(LargeBinary, nullable=False)
# # init_db()
# # # 启动代码解释器容器
# # def start_container(port):
# # container_name = f"code_interpreter_docker_{uuid.uuid4()}"
# # run_command = [
# # "docker", "run", "--name", container_name, "-d", "-p", f"{port}:5000"
# # ]
# # if MEMORY_LIMIT:
# # run_command.extend(["--memory", MEMORY_LIMIT])
# # if CPU_LIMIT:
# # run_command.extend(["--cpus", CPU_LIMIT])
# # run_command.append(INTERPRETER_IMAGE)
# # subprocess.run(run_command, check=True)
# # print(f"Started container {container_name} on port {port}")
# # return container_name, port
# # def stop_container(container_name):
# # subprocess.run(["docker", "stop", container_name])
# # subprocess.run(["docker", "rm", container_name])
# # print(f"Stopped and removed container {container_name}")
# # def start_containers():
# # for port in ports:
# # container_name, port = start_container(port)
# # containers.append((container_name, port))
# # def stop_containers():
# # for container_name, _ in containers:
# # stop_container(container_name)
# # @app.route('/runcode', methods=['POST', 'GET'])
# # def run_code():
# # if request.method == 'POST':
# # data = request.get_json()
# # if not data:
# # return jsonify({'error': 'Invalid JSON'}), 400
# # elif request.method == 'GET':
# # query_string = request.query_string.decode('utf-8')
# # data = {'query_string': query_string}
# # else:
# # return jsonify({'error': 'Invalid request method'}), 405
# # request_id = str(uuid.uuid4()) # 生成一个唯一的请求ID
# # request_queue.put((request_id, data))
# # start_time = time.time()
# # while time.time() - start_time < 10: # 最多等待10秒
# # if request_id in result_dict:
# # output = result_dict.pop(request_id)
# # # 处理返回结果,替换 base64 数据为链接
# # if 'images' in output:
# # try:
# # output = process_images(output)
# # except Exception as e:
# # return jsonify({'error': str(e)}), 500
# # return jsonify(output), 200
# # time.sleep(0.1)
# # return jsonify({'error': 'Request timed out'}), 504
# # def process_images(output):
# # images = output['images']
# # for filename, base64_data in images.items():
# # image_data = base64.b64decode(base64_data)
# # unique_filename = f"{uuid.uuid4()}_{filename}"
# # image_record = Image(filename=unique_filename, data=image_data)
# # try:
# # session.add(image_record)
# # session.commit()
# # except IntegrityError as e:
# # session.rollback()
# # print(f"Database error: {e}")
# # continue
# # if DOMAIN:
# # images[filename] = f"https://{DOMAIN}/image/{unique_filename}"
# # else:
# # images[filename] = f"http://127.0.0.1:{SCHEDULER_PORT}/image/{unique_filename}"
# # return output
# # @app.route('/image/<filename>', methods=['GET'])
# # def serve_image(filename):
# # image_record = session.query(Image).filter_by(filename=filename).first()
# # if image_record:
# # return image_record.data, 200, {'Content-Type': 'image/png'}
# # return jsonify({'error': 'Image not found'}), 404
# # def handle_requests():
# # global current_container_index
# # while True:
# # request_id, data = request_queue.get()
# # if data is None:
# # break
# # semaphore.acquire() # 获取信号量,确保不超过并发限制
# # with lock:
# # container_name, port = containers[current_container_index]
# # current_container_index = (current_container_index + 1) % len(containers)
# # try:
# # if 'query_string' in data:
# # response = requests.get(f"http://localhost:{port}/runcode?{data['query_string']}", timeout=TIMEOUT_SECONDS)
# # else:
# # response = requests.post(f"http://localhost:{port}/runcode", json=data, timeout=TIMEOUT_SECONDS)
# # output = response.json()
# # except requests.exceptions.Timeout:
# # output = {'error': 'Code execution timed out'}
# # except Exception as e:
# # output = {'error': str(e)}
# # result_dict[request_id] = output # 将结果放入结果字典
# # request_queue.task_done()
# # print(f"Finished processing request {request_id}: {output}")
# # # 异步重置容器
# # reset_container(container_name, port)
# # def reset_container(container_name, port):
# # threading.Thread(target=_reset_container, args=(container_name, port)).start()
# # def _reset_container(container_name, port):
# # print(f"Resetting container {container_name}")
# # stop_container(container_name)
# # new_container_name, _ = start_container(port)
# # with lock:
# # for i, (name, p) in enumerate(containers):
# # if name == container_name:
# # containers[i] = (new_container_name, port)
# # break
# # semaphore.release() # 释放信号量
# # def signal_handler(signal, frame):
# # print('Stopping containers and exiting program...')
# # stop_containers()
# # if session:
# # session.close()
# # engine.dispose()
# # os._exit(0)
# # signal.signal(signal.SIGINT, signal_handler)
# # worker_thread = threading.Thread(target=handle_requests)
# # worker_thread.start()
# # start_containers()
# # atexit.register(stop_containers)
# # if __name__ == '__main__':
# # app.run(port=SCHEDULER_PORT, threaded=True) # 使用调度中心端口
# import yaml
# import subprocess
# import uuid
# from flask import Flask, request, jsonify
# from queue import Queue
# import requests
# import threading
# import time
# import os
# import atexit
# import base64
# import signal
# from sqlalchemy import create_engine, Column, String, LargeBinary, Integer
# from sqlalchemy.orm import declarative_base, sessionmaker
# from sqlalchemy.exc import OperationalError, IntegrityError
# import psycopg2
# app = Flask(__name__)
# # 读取配置文件,指定编码为 utf-8
# with open('config.yaml', 'r', encoding='utf-8') as file:
# config = yaml.safe_load(file)
# DOMAIN = config['domain']
# INTERPRETER_IMAGE = config['interpreter_image']
# PORT_START = config['interpreter_port_range']['start']
# PORT_END = config['interpreter_port_range']['end']
# DEPENDENCIES = config['dependencies']
# RESOURCE_LIMITS = config.get('resource_limits', {})
# MEMORY_LIMIT = RESOURCE_LIMITS.get('memory')
# CPU_LIMIT = RESOURCE_LIMITS.get('cpus')
# POSTGRES = config['postgres']
# TIMEOUT = config.get('timeout', 30) # 默认超时时间为30秒
# TIMEOUT_SECONDS = config.get('timeout_seconds', 60) # 从配置中读取超时时间
# SCHEDULER_PORT = config['scheduler_port'] # 新增调度中心端口
# # 容器管理
# containers = []
# ports = list(range(PORT_START, PORT_END + 1))
# lock = threading.Lock()
# request_queue = Queue()
# result_dict = {} # 用于存储结果的字典
# current_container_index = 0
# semaphore = threading.Semaphore(len(ports)) # 控制并发请求的信号量
# # 检查 PostgreSQL 容器是否存在
# POSTGRES_CONTAINER_NAME = "postgres_code_interpreter"
# POSTGRES_DATA_DIR = os.path.join(os.getcwd(), 'pgdata')
# def is_postgres_container_running():
# result = subprocess.run(["docker", "ps", "-a", "--filter", f"name={POSTGRES_CONTAINER_NAME}", "--format", "{{.Names}}"], capture_output=True, text=True)
# return POSTGRES_CONTAINER_NAME in result.stdout.strip().split('\n')
# def wait_for_postgres_ready():
# retry_attempts = 10
# retry_delay = 5 # seconds
# for attempt in range(retry_attempts):
# try:
# conn = psycopg2.connect(
# dbname=POSTGRES['db'],
# user=POSTGRES['user'],
# password=POSTGRES['password'],
# host=POSTGRES['host'],
# port=POSTGRES['port']
# )
# conn.close()
# print("PostgreSQL is ready")
# return True
# except psycopg2.OperationalError as e:
# print(f"Waiting for PostgreSQL to be ready: {e}")
# time.sleep(retry_delay)
# return False
# if not is_postgres_container_running():
# if not os.path.exists(POSTGRES_DATA_DIR):
# os.makedirs(POSTGRES_DATA_DIR)
# subprocess.run([
# "docker", "run", "--name", POSTGRES_CONTAINER_NAME, "-d",
# "-e", f"POSTGRES_USER={POSTGRES['user']}",
# "-e", f"POSTGRES_PASSWORD={POSTGRES['password']}",
# "-e", f"POSTGRES_DB={POSTGRES['db']}",
# "-v", f"{POSTGRES_DATA_DIR}:/var/lib/postgresql/data",
# "-p", f"{POSTGRES['port']}:5432",
# "postgres"
# ], check=True)
# if not wait_for_postgres_ready():
# print("Failed to connect to PostgreSQL after multiple attempts.")
# exit(1)
# # PostgreSQL 配置
# DATABASE_URL = f"postgresql://{POSTGRES['user']}:{POSTGRES['password']}@{POSTGRES['host']}:{POSTGRES['port']}/{POSTGRES['db']}"
# engine = None
# Base = declarative_base()
# Session = None
# session = None
# def init_db():
# global engine, Session, session
# retry_attempts = 5
# retry_delay = 5 # seconds
# for attempt in range(retry_attempts):
# try:
# engine = create_engine(DATABASE_URL)
# Session = sessionmaker(bind=engine)
# session = Session()
# Base.metadata.create_all(engine) # 确保数据库模式已创建
# print("Database connection successful")
# break
# except OperationalError as e:
# print(f"Database connection failed: {e}")
# if attempt < retry_attempts - 1:
# print(f"Retrying in {retry_delay} seconds...")
# time.sleep(retry_delay)
# else:
# print("Failed to connect to the database after multiple attempts.")
# raise
# class Image(Base):
# __tablename__ = 'images'
# id = Column(Integer, primary_key=True, autoincrement=True)
# filename = Column(String, unique=True, nullable=False)
# data = Column(LargeBinary, nullable=False)
# init_db()
# # 启动代码解释器容器
# def start_container(port):
# container_name = f"code_interpreter_docker_{uuid.uuid4()}"
# run_command = [
# "docker", "run", "--name", container_name, "-d", "-p", f"{port}:5000"
# ]
# if MEMORY_LIMIT:
# run_command.extend(["--memory", MEMORY_LIMIT])
# if CPU_LIMIT:
# run_command.extend(["--cpus", CPU_LIMIT])
# run_command.append(INTERPRETER_IMAGE)
# subprocess.run(run_command, check=True)
# print(f"Started container {container_name} on port {port}")
# return container_name, port
# def stop_container(container_name):
# subprocess.run(["docker", "stop", container_name])
# subprocess.run(["docker", "rm", container_name])
# print(f"Stopped and removed container {container_name}")
# def start_containers():
# for port in ports:
# container_name, port = start_container(port)
# containers.append((container_name, port))
# def stop_containers():
# for container_name, _ in containers:
# stop_container(container_name)
# @app.route('/runcode', methods=['POST', 'GET'])
# def run_code():
# if request.method == 'POST':
# data = request.get_json()
# if not data:
# return jsonify({'error': 'Invalid JSON'}), 400
# elif request.method == 'GET':
# query_string = request.query_string.decode('utf-8')
# data = {'query_string': query_string}
# else:
# return jsonify({'error': 'Invalid request method'}), 405
# request_id = str(uuid.uuid4()) # 生成一个唯一的请求ID
# request_queue.put((request_id, data))
# start_time = time.time()
# while time.time() - start_time < 10: # 最多等待10秒
# if request_id in result_dict:
# output = result_dict.pop(request_id)
# # 处理返回结果,替换 base64 数据为链接
# if 'images' in output:
# try:
# output = process_images(output)
# except Exception as e:
# return jsonify({'error': str(e)}), 500
# return jsonify(output), 200
# time.sleep(0.1)
# return jsonify({'error': 'Request timed out'}), 504
# def process_images(output):
# images = output['images']
# for filename, base64_data in images.items():
# image_data = base64.b64decode(base64_data)
# unique_filename = f"{uuid.uuid4()}_{filename}"
# image_record = Image(filename=unique_filename, data=image_data)
# try:
# session.add(image_record)
# session.commit()
# except IntegrityError as e:
# session.rollback()
# print(f"Database error: {e}")
# continue
# if DOMAIN:
# images[filename] = f"https://{DOMAIN}/image/{unique_filename}"
# else:
# images[filename] = f"http://127.0.0.1:{SCHEDULER_PORT}/image/{unique_filename}"
# return output
# @app.route('/image/<filename>', methods=['GET'])
# def serve_image(filename):
# image_record = session.query(Image).filter_by(filename=filename).first()
# if image_record:
# return image_record.data, 200, {'Content-Type': 'image/png'}
# return jsonify({'error': 'Image not found'}), 404
# def handle_requests():
# global current_container_index
# while True:
# request_id, data = request_queue.get()
# if data is None:
# break
# semaphore.acquire() # 获取信号量,确保不超过并发限制
# with lock:
# container_name, port = containers[current_container_index]
# current_container_index = (current_container_index + 1) % len(containers)
# try:
# if 'query_string' in data:
# response = requests.get(f"http://localhost:{port}/runcode?{data['query_string']}", timeout=TIMEOUT_SECONDS)
# else:
# response = requests.post(f"http://localhost:{port}/runcode", json=data, timeout=TIMEOUT_SECONDS)
# output = response.json()
# except requests.exceptions.Timeout:
# output = {'error': 'Code execution timed out'}
# except Exception as e:
# output = {'error': str(e)}
# result_dict[request_id] = output # 将结果放入结果字典
# request_queue.task_done()
# print(f"Finished processing request {request_id}: {output}")
# # 异步重置容器
# reset_container(container_name, port)
# def reset_container(container_name, port):
# threading.Thread(target=_reset_container, args=(container_name, port)).start()
# def _reset_container(container_name, port):
# print(f"Resetting container {container_name}")
# stop_container(container_name)
# new_container_name, _ = start_container(port)
# with lock:
# for i, (name, p) in enumerate(containers):
# if name == container_name:
# containers[i] = (new_container_name, port)
# break
# semaphore.release() # 释放信号量
# def signal_handler(signal, frame):
# print('Stopping containers and exiting program...')
# stop_containers()
# if session:
# session.close()
# engine.dispose()
# os._exit(0)
# signal.signal(signal.SIGINT, signal_handler)
# # 启动多个工作线程来处理请求
# for _ in range(len(ports)):
# threading.Thread(target=handle_requests).start()
# start_containers()
# atexit.register(stop_containers)
# if __name__ == '__main__':
# app.run(port=SCHEDULER_PORT, threaded=True) # 使用调度中心端口
import yaml
import subprocess
import uuid
from flask import Flask, request, jsonify
from queue import Queue
import requests
import threading
import time
import os
import atexit
import base64
import signal
from sqlalchemy import create_engine, Column, String, LargeBinary, Integer
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.exc import OperationalError, IntegrityError
import psycopg2
app = Flask(__name__)
# 读取配置文件,指定编码为 utf-8
with open('config.yaml', 'r', encoding='utf-8') as file:
config = yaml.safe_load(file)
DOMAIN = config['domain']
INTERPRETER_IMAGE = config['interpreter_image']
PORT_START = config['interpreter_port_range']['start']
PORT_END = config['interpreter_port_range']['end']
DEPENDENCIES = config['dependencies']
RESOURCE_LIMITS = config.get('resource_limits', {})
MEMORY_LIMIT = RESOURCE_LIMITS.get('memory')
CPU_LIMIT = RESOURCE_LIMITS.get('cpus')
POSTGRES = config['postgres']
TIMEOUT = config.get('timeout', 30) # 默认超时时间为30秒
TIMEOUT_SECONDS = config.get('timeout_seconds', 60) # 从配置中读取超时时间
SCHEDULER_PORT = config['scheduler_port'] # 新增调度中心端口
# 容器管理
containers = []
ports = list(range(PORT_START, PORT_END + 1))
lock = threading.Lock()
request_queue = Queue()
result_dict = {} # 用于存储结果的字典
current_container_index = 0
semaphore = threading.Semaphore(len(ports)) # 控制并发请求的信号量
# 检查 PostgreSQL 容器是否存在
POSTGRES_CONTAINER_NAME = "postgres_code_interpreter"
POSTGRES_DATA_DIR = os.path.join(os.getcwd(), 'pgdata')
def is_postgres_container_running():
result = subprocess.run(["docker", "ps", "-a", "--filter", f"name={POSTGRES_CONTAINER_NAME}", "--format", "{{.Names}}"], capture_output=True, text=True)
return POSTGRES_CONTAINER_NAME in result.stdout.strip().split('\n')
def wait_for_postgres_ready():
retry_attempts = 10
retry_delay = 5 # seconds
for attempt in range(retry_attempts):
try:
conn = psycopg2.connect(
dbname=POSTGRES['db'],
user=POSTGRES['user'],
password=POSTGRES['password'],
host=POSTGRES['host'],
port=POSTGRES['port']
)
conn.close()
print("PostgreSQL is ready")
return True
except psycopg2.OperationalError as e:
print(f"Waiting for PostgreSQL to be ready: {e}")
time.sleep(retry_delay)
return False
if not is_postgres_container_running():
if not os.path.exists(POSTGRES_DATA_DIR):
os.makedirs(POSTGRES_DATA_DIR)
subprocess.run([
"docker", "run", "--name", POSTGRES_CONTAINER_NAME, "-d",
"-e", f"POSTGRES_USER={POSTGRES['user']}",
"-e", f"POSTGRES_PASSWORD={POSTGRES['password']}",
"-e", f"POSTGRES_DB={POSTGRES['db']}",
"-v", f"{POSTGRES_DATA_DIR}:/var/lib/postgresql/data",
"-p", f"{POSTGRES['port']}:5432",
"postgres"
], check=True)
if not wait_for_postgres_ready():
print("Failed to connect to PostgreSQL after multiple attempts.")
exit(1)
# PostgreSQL 配置
DATABASE_URL = f"postgresql://{POSTGRES['user']}:{POSTGRES['password']}@{POSTGRES['host']}:{POSTGRES['port']}/{POSTGRES['db']}"
engine = None
Base = declarative_base()
Session = None
session = None
def init_db():
global engine, Session, session
retry_attempts = 5
retry_delay = 5 # seconds
for attempt in range(retry_attempts):
try:
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session = Session()
Base.metadata.create_all(engine) # 确保数据库模式已创建
print("Database connection successful")
break
except OperationalError as e:
print(f"Database connection failed: {e}")
if attempt < retry_attempts - 1:
print(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
print("Failed to connect to the database after multiple attempts.")
raise
class Image(Base):
__tablename__ = 'images'
id = Column(Integer, primary_key=True, autoincrement=True)
filename = Column(String, unique=True, nullable=False)
data = Column(LargeBinary, nullable=False)
init_db()
# 启动代码解释器容器
def start_container(port):
container_name = f"code_interpreter_docker_{uuid.uuid4()}"
run_command = [
"docker", "run", "--name", container_name, "-d", "-p", f"{port}:5000"
]
if MEMORY_LIMIT:
run_command.extend(["--memory", MEMORY_LIMIT])
if CPU_LIMIT:
run_command.extend(["--cpus", CPU_LIMIT])
run_command.append(INTERPRETER_IMAGE)
subprocess.run(run_command, check=True)
print(f"Started container {container_name} on port {port}")
return container_name, port
def stop_container(container_name):
subprocess.run(["docker", "stop", container_name])
subprocess.run(["docker", "rm", container_name])
print(f"Stopped and removed container {container_name}")
def start_containers():
for port in ports:
container_name, port = start_container(port)
containers.append((container_name, port))
def stop_containers():
for container_name, _ in containers:
stop_container(container_name)
@app.route('/runcode', methods=['POST', 'GET'])
def run_code():
if request.method == 'POST':
data = request.get_json()
if not data:
return jsonify({'error': 'Invalid JSON'}), 400
elif request.method == 'GET':
query_string = request.query_string.decode('utf-8')
data = {'query_string': query_string}
else:
return jsonify({'error': 'Invalid request method'}), 405
request_id = str(uuid.uuid4()) # 生成一个唯一的请求ID
request_queue.put((request_id, data))
start_time = time.time()
while time.time() - start_time < 10: # 最多等待10秒
if request_id in result_dict:
output = result_dict.pop(request_id)
# 处理返回结果,替换 base64 数据为链接
if 'images' in output:
try:
output = process_images(output)
except Exception as e:
return jsonify({'error': str(e)}), 500
return jsonify(output), 200
time.sleep(0.1)
return jsonify({'error': 'Request timed out'}), 504
def process_images(output):
images = output['images']
for filename, base64_data in images.items():
image_data = base64.b64decode(base64_data)
unique_filename = f"{uuid.uuid4()}_{filename}"
image_record = Image(filename=unique_filename, data=image_data)
try:
session.add(image_record)
session.commit()
except IntegrityError as e:
session.rollback()
print(f"Database error: {e}")
continue
if DOMAIN:
images[filename] = f"https://{DOMAIN}/image/{unique_filename}"
else:
images[filename] = f"http://127.0.0.1:{SCHEDULER_PORT}/image/{unique_filename}"
return output
@app.route('/image/<filename>', methods=['GET'])
def serve_image(filename):
image_record = session.query(Image).filter_by(filename=filename).first()
if image_record:
return image_record.data, 200, {'Content-Type': 'image/png'}
return jsonify({'error': 'Image not found'}), 404
def handle_requests():
global current_container_index
while True:
request_id, data = request_queue.get()
if data is None:
break
semaphore.acquire() # 获取信号量,确保不超过并发限制
with lock:
container_name, port = containers[current_container_index]
current_container_index = (current_container_index + 1) % len(containers)
try:
if 'query_string' in data:
response = requests.get(f"http://localhost:{port}/runcode?{data['query_string']}", timeout=TIMEOUT_SECONDS)
else:
response = requests.post(f"http://localhost:{port}/runcode", json=data, timeout=TIMEOUT_SECONDS)
output = response.json()
except requests.exceptions.Timeout:
output = {'error': 'Code execution timed out'}
except Exception as e:
output = {'error': str(e)}
result_dict[request_id] = output # 将结果放入结果字典
request_queue.task_done()
print(f"Finished processing request {request_id}: {output}")
# 异步重置容器
reset_container(container_name, port)
def reset_container(container_name, port):
threading.Thread(target=_reset_container, args=(container_name, port)).start()
def _reset_container(container_name, port):
print(f"Resetting container {container_name}")
stop_container(container_name)
new_container_name, _ = start_container(port)
with lock:
for i, (name, p) in enumerate(containers):
if name == container_name:
containers[i] = (new_container_name, port)
break
semaphore.release() # 释放信号量
def signal_handler(signal, frame):
print('Stopping containers and exiting program...')
stop_containers()
if session:
session.close()
engine.dispose()
os._exit(0)
signal.signal(signal.SIGINT, signal_handler)
# 启动多个工作线程来处理请求
for _ in range(len(ports)):
threading.Thread(target=handle_requests).start()
start_containers()
atexit.register(stop_containers)
if __name__ == '__main__':
app.run(port=SCHEDULER_PORT, threaded=True) # 使用调度中心端口1234567891011121314151617181920212223242526272829303132import aiohttp
import asyncio
async def post_request(session, url, headers, json_data):
async with session.post(url, headers=headers, json=json_data) as response:
response_text = await response.text()
print(response_text)
return response_text
async def main(concurrent_requests):
url = 'http://127.0.0.1:8000/runcode'
headers = {
'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
'Content-Type': 'application/json',
'Authorization': 'Bearer 114514',
'Accept': '*/*',
'Host': '127.0.0.1:8000',
'Connection': 'keep-alive'
}
json_data = {
"languageType": "python",
"variables": {},
"code": "```python\nimport time\nprint('Hello from code block!')\ntime.sleep(3)\n```"
}
async with aiohttp.ClientSession() as session:
tasks = [post_request(session, url, headers, json_data) for _ in range(concurrent_requests)]
await asyncio.gather(*tasks)
if __name__ == '__main__':
concurrent_requests = 5 # 设置并发数
asyncio.run(main(concurrent_requests))1234567891011121314151617181920212223242526272829303132333435363738394041424344domain: "" #填写域名后对外提供链接将使用域名替代 ip:scheduler_port
interpreter_image: "leezhuuu/code_interpreter"
scheduler_port: 8000 # 调度中心端口
interpreter_port_range: # 设置端口范围为 8001-8003,端口范围决定容器数量
start: 8001
end: 8003
dependencies: # 定义容器的依赖项,以下为默认依赖项,可以根据需要自行更改
- numpy
- pandas
- scipy
- matplotlib
- seaborn
- scikit-learn
- opencv-python
- opencv-python-headless
- Pillow
- requests
- Flask
- pyyaml
- sympy
- plotly
- bokeh
- statsmodels
- jupyter
- ipython
- jupyterlab
- pytest
- hypothesis
- Flask-Cors
- Werkzeug
- Gunicorn
resource_limits:
memory: "" # 可以自定义内存限制,例如 "2g"、"500m" 等,不填则默认不进行限制
cpus: "" # 可以自定义 CPU 限制,例如 "1.5"、"0.5" 等,不填则默认不进行限制
timeout_seconds: 60 # 设置超时时间为 1 分钟(60 秒)
postgres:
user: "user" # 数据库用户名,请自行更改
password: "password" # 数据库密码,请自行更改
db: "code_interpreter_db"
host: "localhost"
port: "5432" # 数据库端口,请自行更改
mode: "docker" # 选择运行模式,可以是 "docker" 或 "k8s"
kubeconfig_path: "/path/to/kubeconfig.yaml" # Kubernetes 配置文件路径
k8s_interpreter_yaml: "/path/to/code-interpreter.yaml" # Kubernetes 部署配置文件路径123456789101112131415161718192021222324numpy
pandas
scipy
matplotlib
seaborn
scikit-learn
opencv-python
opencv-python-headless
Pillow
requests
Flask
pyyaml
sympy
plotly
bokeh
statsmodels
jupyter
ipython
jupyterlab
pytest
hypothesis
Flask-Cors
Werkzeug
GunicornDockerfileAdded+38
app.pyAdded+251
build.pyAdded+27
center.pyAdded+1392
concurrent_test.pyAdded+32
config.yamlAdded+44
requirements.txtAdded+24