Skip to content

Commit e2caad5

Browse files
committed
* Introduced retries on connecting
* Modified tests, sending 50 commands now, one after another, as a stress test * Added a shutdown of connection before closing [link](https://docs.python.org/2/library/socket.html#socket.socket.close) * Added a 1s. timeout
1 parent b59ad87 commit e2caad5

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

command_port.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def run(self):
107107
connection.sendall(output.encode())
108108
else:
109109
connection.sendall('OK'.encode())
110+
connection.shutdown(socket.SHUT_RDWR)
110111
connection.close()
111112
except socket.timeout:
112113
pass
@@ -190,7 +191,7 @@ def execute(self, context):
190191
exec(command, globals(), _locals)
191192
globals().update(_locals)
192193
else:
193-
exec(command, _globals, {})
194+
exec(command, globals(), {})
194195
result = output_duplicator.last_line
195196
except Exception as e:
196197
result = '\n'.join([str(v) for v in e.args])

execute_file_in_blender.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
import socket
22
import sys
33
import os
4+
import time
45

56

7+
# noinspection PyUnboundLocalVariable
68
def send_command(command, host='localhost', port=None):
79
if port is None:
810
port = sys.argv[2]
911

1012
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
11-
clientsocket.connect((host, int(port)))
13+
for i in range(3):
14+
# Retry 3 times, wait from 0 to .5 s. before retries, raise last Exception, if no success
15+
# noinspection PyBroadException
16+
try:
17+
clientsocket.connect((host, int(port)))
18+
break
19+
except Exception as e:
20+
time.sleep(i / 2.0)
21+
else:
22+
raise e
1223
clientsocket.sendall(command.encode())
24+
clientsocket.settimeout(1)
1325
result = ""
1426
while True:
1527
res = clientsocket.recv(4096)
1628
if not res:
1729
break
1830
print(res.decode())
1931
result += res.decode()
32+
clientsocket.shutdown(socket.SHUT_RDWR)
2033
clientsocket.close()
2134
return result
2235

tests/test_blender_command_port.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ class TestCommandPort(TestCase):
1111
Port need to be opened in Blender with port 5000 on localhost and "Share environment" set to True.
1212
"""
1313
def test_send_command(self):
14-
send_command("""print("test passed")""", port=5000)
14+
result = send_command("""print("TEST PASSED")""", port=5000)
15+
self.assertTrue("TEST PASSED" in result)
16+
17+
for i in range(30):
18+
send_command("""print("test passed")""", port=5000)
19+
result = send_command("""print("TEST PASSED")""", port=5000)
20+
self.assertTrue("TEST PASSED" in result)
1521

1622
def test_execute_file(self):
1723
with NamedTemporaryFile(mode="w", suffix='.py', delete=True) as f:

0 commit comments

Comments
 (0)