Skip to content

Commit e00e17b

Browse files
authored
Flush STDOUT when server starts listening. (ggml-org#651)
This works around a Win32 issue when piping output from a PyInstaller context, such as when doing so in a perl script or to an output file. Print statements from a Python context don't properly get output unless flushed. This strategically flushes the print statements so no information is lost, though it may be better to flush all print statements in a Python context via a subroutine wrapper. See also: https://mail.python.org/pipermail/python-bugs-list/2004-August/024923.html https://stackoverflow.com/a/466849 https://stackoverflow.com/q/62693079
1 parent 8c22f10 commit e00e17b

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

koboldcpp.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,9 @@ def main(launch_args,start_server=True):
24332433

24342434
modelname = os.path.abspath(args.model_param)
24352435
print(args)
2436-
print(f"==========\nLoading model: {modelname} \n[Threads: {args.threads}, BlasThreads: {args.blasthreads}, SmartContext: {args.smartcontext}, ContextShift: {not (args.noshift)}]")
2436+
# Flush stdout for win32 issue with regards to piping in terminals,
2437+
# especially before handing over to C++ context.
2438+
print(f"==========\nLoading model: {modelname} \n[Threads: {args.threads}, BlasThreads: {args.blasthreads}, SmartContext: {args.smartcontext}, ContextShift: {not (args.noshift)}]", flush=True)
24372439
loadok = load_model(modelname)
24382440
print("Load Model OK: " + str(loadok))
24392441

@@ -2507,10 +2509,12 @@ def onready_subprocess():
25072509
if start_server:
25082510
if args.remotetunnel:
25092511
setuptunnel()
2510-
print(f"======\nPlease connect to custom endpoint at {epurl}")
2512+
# Flush stdout for previous win32 issue so the client can see output.
2513+
print(f"======\nPlease connect to custom endpoint at {epurl}", flush=True)
25112514
asyncio.run(RunServerMultiThreaded(args.host, args.port, embedded_kailite, embedded_kcpp_docs))
25122515
else:
2513-
print(f"Server was not started, main function complete. Idling.")
2516+
# Flush stdout for previous win32 issue so the client can see output.
2517+
print(f"Server was not started, main function complete. Idling.", flush=True)
25142518

25152519
def run_in_queue(launch_args, input_queue, output_queue):
25162520
main(launch_args, start_server=False)

0 commit comments

Comments
 (0)