Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Run chdb.query without fork
Browse files Browse the repository at this point in the history
  • Loading branch information
auxten committed Jul 17, 2023
1 parent b722682 commit 74c642b
Showing 1 changed file with 26 additions and 31 deletions.
57 changes: 26 additions & 31 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
import os
import select
import tempfile

import chdb
from flask import Flask, request

app = Flask(__name__, static_folder="public", static_url_path="")


# run chdb.query(query, format), get result from return and collect stderr
def chdb_query_with_errmsg(query, format):
pipe_out, pipe_in = os.pipe()
stderr = os.dup(2)
os.dup2(pipe_in, 2)

# check if we have more to read from the pipe
def more_data():
r, _, _ = select.select([pipe_out], [], [], 0)
return bool(r)

# read the whole pipe
def read_pipe():
out = b''
while more_data():
out += os.read(pipe_out, 1024)

return out.decode(encoding='utf-8', errors='strict')

res = chdb.query(query, format)
os.dup2(stderr, 2)

result = res.get_memview().tobytes()
errmsg = read_pipe()
return result, errmsg

# Redirect stdout and stderr to the buffers
try:
new_stderr = tempfile.TemporaryFile()
old_stderr_fd = os.dup(2)
os.dup2(new_stderr.fileno(), 2)
# Call the function
output = chdb.query(query, format).bytes()

new_stderr.flush()
new_stderr.seek(0)
errmsg = new_stderr.read()

# cleanup and recover
new_stderr.close()
os.dup2(old_stderr_fd, 2)
except Exception as e:
# An error occurred, print it to stderr
print(f"An error occurred: {e}")
return output, errmsg


@app.route('/', methods=["GET"])
def clickhouse():
Expand All @@ -41,10 +38,9 @@ def clickhouse():
return app.send_static_file('play.html')

result, errmsg = chdb_query_with_errmsg(query, format)
if errmsg == '':
if len(errmsg) == 0:
return result
else:
return errmsg
return errmsg


@app.route('/', methods=["POST"])
Expand All @@ -55,10 +51,9 @@ def play():
return app.send_static_file('play.html')

result, errmsg = chdb_query_with_errmsg(query, format)
if errmsg == '':
if len(errmsg) == 0:
return result
else:
return errmsg
return errmsg


@app.errorhandler(404)
Expand Down

0 comments on commit 74c642b

Please sign in to comment.