Skip to content

Commit 2c200c4

Browse files
author
ac133451
committed
- added daemon option
- added sys.exit() to examples
1 parent 598fe3e commit 2c200c4

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

DataSocket/TCPDataSocket.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ def _get_socket():
2222

2323

2424
class TCPSendSocket(object):
25-
def __init__(self, tcp_port, tcp_ip='localhost', send_type=NUMPY, verbose=True, as_server=True, include_time=False):
25+
def __init__(self,
26+
tcp_port,
27+
tcp_ip='localhost',
28+
send_type=NUMPY,
29+
verbose=True,
30+
as_server=True,
31+
include_time=False,
32+
as_daemon=True):
2633
"""
2734
A TCP socket class to send data to a specific port and address.
2835
:param tcp_port: TCP port to use.
@@ -36,6 +43,7 @@ def __init__(self, tcp_port, tcp_ip='localhost', send_type=NUMPY, verbose=True,
3643
:param as_server: Whether to run this socket as a server (default: True) or client. When run as a server, the
3744
socket supports multiple clients and sends each message to every connected client.
3845
:param include_time: Appends time.time() value when sending the data message.
46+
:param as_daemon: runs the underlying threads as daemon.
3947
"""
4048
self.send_type = send_type
4149
self.data_to_send = b'0'
@@ -48,8 +56,8 @@ def __init__(self, tcp_port, tcp_ip='localhost', send_type=NUMPY, verbose=True,
4856
self.as_server = as_server
4957
self.include_time = include_time
5058
self.connected_clients = []
51-
self._gather_connections_thread = Thread(target=self._gather_connections)
52-
self.sending_thread = Thread(target=self._run)
59+
self._gather_connections_thread = Thread(target=self._gather_connections, daemon=as_daemon)
60+
self.sending_thread = Thread(target=self._run, daemon=as_daemon)
5361

5462
def send_data(self, data):
5563
"""
@@ -242,7 +250,8 @@ def __init__(self,
242250
verbose=True,
243251
as_server=False,
244252
receive_as_raw=False,
245-
receive_buffer_size=4095):
253+
receive_buffer_size=4095,
254+
as_daemon=True):
246255
"""
247256
Receiving TCP socket to be used with TCPSendSocket.
248257
:param tcp_port: TCP port to use.
@@ -257,6 +266,7 @@ def my_handler(received_data):
257266
whatever the SendSocket is configured to be.
258267
:param receive_as_raw: Whether or not the incoming data is just raw bytes or is a predefined format (JSON, NUMPY, HDF)
259268
:param receive_buffer_size: available buffer size in bytes when receiving messages
269+
:param as_daemon: runs underlying threads as daemon.
260270
"""
261271
self.receive_buffer_size = receive_buffer_size
262272
self.receive_as_raw = receive_as_raw
@@ -274,9 +284,9 @@ def pass_func(data):
274284
self._new_data = None
275285
self._new_data_lock = Lock()
276286
self.new_data_flag = Event()
277-
self.handler_thread = Thread(target=self._handler, daemon=True)
287+
self.handler_thread = Thread(target=self._handler, daemon=as_daemon)
278288
self.socket = _get_socket()
279-
self.thread = Thread(target=self._run, daemon=True)
289+
self.thread = Thread(target=self._run, daemon=as_daemon)
280290
self.port = int(tcp_port)
281291
self.ip = tcp_ip
282292
self.block_size = 0
@@ -289,8 +299,7 @@ def pass_func(data):
289299
def start(self, blocking=False):
290300
"""
291301
Start the socket service.
292-
:param blocking: Will block the calling thread until a connection is established to at least one receiver.
293-
:return: Nothing
302+
:param blocking: Will block the calling thread until a connection is established.
294303
"""
295304
self.thread.start()
296305
if blocking:

examples/hdp_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from DataSocket import TCPSendSocket, TCPReceiveSocket, HDF
22
import time
33
from threading import Thread
4+
import sys
45

56

67
number_of_messages = 5 # number of sample messages to send
@@ -50,3 +51,5 @@ def print_value(data):
5051

5152
send_thread.join()
5253
rec_thread.join()
54+
55+
sys.exit()

examples/json_example.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from DataSocket import TCPSendSocket, TCPReceiveSocket, JSON
22
import time
33
from threading import Thread
4+
import sys
45

56

67
number_of_messages = 5 # number of sample messages to send
@@ -53,3 +54,5 @@ def print_value(data):
5354
send_thread.join()
5455
rec_thread1.join()
5556
rec_thread2.join()
57+
58+
sys.exit()

examples/numpy_example.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from DataSocket import TCPSendSocket, TCPReceiveSocket, NUMPY
22
import time
33
from threading import Thread
4+
import sys
5+
import numpy as np
46

57

68
number_of_messages = 5 # number of sample messages to send
@@ -13,7 +15,8 @@ def sending_function():
1315
send_socket.start(blocking=True)
1416

1517
for i in range(number_of_messages):
16-
send_socket.send_data(i*10)
18+
send_socket.send_data({'i': i*10,
19+
'data': np.random.random(4)})
1720
time.sleep(0.25)
1821

1922
print("closing send socket.")
@@ -26,7 +29,7 @@ def receiving_function():
2629

2730
# function to run when a new piece of data is received
2831
def print_value(data):
29-
print("value received: ", data['data'])
32+
print('i=', data['i'], "data received: ", data['data'])
3033
num_messages_received[0] = 1 + num_messages_received[0]
3134

3235
rec_socket = TCPReceiveSocket(tcp_port=port, handler_function=print_value)
@@ -50,3 +53,4 @@ def print_value(data):
5053

5154
send_thread.join()
5255
rec_thread.join()
56+
sys.exit()

examples/simulink_example.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@
99
receive_port = 4343
1010
ip = '0.0.0.0'
1111

12+
start_time = time.time()
1213

1314
# define function to print the echo back from matlab
1415
def print_data(data):
15-
print('length of returned array:', np.frombuffer(data, dtype='float32').shape[0])
16+
global start_time
17+
now = time.time()
18+
print('length of returned array:', np.frombuffer(data, dtype='float32').shape[0], 'Time for full trip:', now - start_time)
1619

1720

1821
# create a send and receive socket
@@ -27,11 +30,13 @@ def print_data(data):
2730

2831

2932
def send_sig():
33+
global start_time
3034
while not stop_flag.is_set():
3135
data = np.random.random((100, 100)) # create 100x100 array of random numbers
3236
data_as_bytes = data.astype('float32').flatten().tostring() # flatten it before sending
37+
start_time = time.time()
3338
send_socket.send_data(data_as_bytes)
34-
time.sleep(1)
39+
time.sleep(0.5)
3540

3641

3742
thread = threading.Thread(target=send_sig)

0 commit comments

Comments
 (0)