Extensions: resolve reading JSON output blocking Blender

Resolve reading JSON chunks blocking Blender's process by yielding an
empty message when there is no new data read from the process.
This commit is contained in:
Campbell Barton
2024-05-10 14:20:52 +10:00
parent 7b8748f12a
commit def2997f8b

View File

@ -207,19 +207,17 @@ def command_output_from_json_0(
raise ex
chunk = b''
json_messages = []
if not chunk:
if ps.poll() is not None:
break
if use_idle:
time.sleep(IDLE_WAIT_ON_READ)
continue
# Extract contiguous data from `chunk_list`.
chunk_zero_index = chunk.find(b'\0')
if chunk_zero_index == -1:
elif (chunk_zero_index := chunk.find(b'\0')) == -1:
chunk_list.append(chunk)
else:
json_messages = []
# Extract contiguous data from `chunk_list`.
chunk_list.append(chunk[:chunk_zero_index])
json_bytes_list = [b''.join(chunk_list)]
@ -245,10 +243,12 @@ def command_output_from_json_0(
json_messages.append((json_data[0], json_data[1]))
request_exit = yield json_messages
if request_exit and not request_exit_signal_sent:
ps.send_signal(signal.SIGINT)
request_exit_signal_sent = True
# Yield even when `json_messages`, otherwise this generator can block.
# It also means a request to exit might not be responded to soon enough.
request_exit = yield json_messages
if request_exit and not request_exit_signal_sent:
ps.send_signal(signal.SIGINT)
request_exit_signal_sent = True
# -----------------------------------------------------------------------------