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