Skip to content

Commit a28373f

Browse files
authored
Update LDM python samples to use the new wait_for_debug_client logic (#258)
1 parent 3324d1f commit a28373f

File tree

6 files changed

+92
-62
lines changed

6 files changed

+92
-62
lines changed
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
3-
4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
73
# Print the incoming invocation event.
84
print(event)
95

106
# Return the incoming invocation event.
117
return event
128

139

14-
def wait_for_debug_client(timeout=3600):
10+
def wait_for_debug_client(port: int=19891, timeout: int=3600):
1511
"""Utility function to enable debugging with Visual Studio Code"""
12+
1613
import time, threading
1714
import sys, glob
1815
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
1916
import debugpy
2017

21-
debugpy.listen(("0.0.0.0", 19891))
22-
class T(threading.Thread):
23-
daemon = True
24-
def run(self):
18+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
19+
wait_for_debug_client._debugpy_listening = False
20+
21+
if not wait_for_debug_client._debugpy_listening:
22+
try:
23+
debugpy.listen(("0.0.0.0", port))
24+
wait_for_debug_client._debugpy_listening = True
25+
print(f"debugpy is now listening on port {port}")
26+
except RuntimeError as e:
27+
print(f"debugpy.listen() failed or already active: {e}")
28+
29+
if not debugpy.is_client_connected():
30+
print("Waiting for client to attach debugger...")
31+
32+
def cancel_wait():
2533
time.sleep(timeout)
26-
print("Canceling debug wait task ...")
34+
print("Canceling debug wait task after timeout...")
2735
debugpy.wait_for_client.cancel()
28-
T().start()
29-
print("Waiting for client to attach debugger ...")
30-
debugpy.wait_for_client()
31-
3236

33-
if __name__ == "__main__":
34-
handler({}, {})
37+
threading.Thread(target=cancel_wait, daemon=True).start()
38+
debugpy.wait_for_client()
39+
else:
40+
print("Debugger already attached.")
3541

42+
wait_for_debug_client()
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
functions:
22
arn:aws:lambda:us-east-1:000000000000:function:function-one:
33
debug-port: 19891
4-
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
33

4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
74
# Print the incoming invocation event.
85
print(event)
96

107
# Return the incoming invocation event.
118
return event
129

1310

14-
def wait_for_debug_client(timeout=3600):
11+
def wait_for_debug_client(port: int=19891, timeout: int=3600):
1512
"""Utility function to enable debugging with Visual Studio Code"""
13+
1614
import time, threading
1715
import sys, glob
1816
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
1917
import debugpy
2018

21-
debugpy.listen(("0.0.0.0", 19891))
22-
class T(threading.Thread):
23-
daemon = True
24-
def run(self):
19+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
20+
wait_for_debug_client._debugpy_listening = False
21+
22+
if not wait_for_debug_client._debugpy_listening:
23+
try:
24+
debugpy.listen(("0.0.0.0", port))
25+
wait_for_debug_client._debugpy_listening = True
26+
print(f"debugpy is now listening on port {port}")
27+
except RuntimeError as e:
28+
print(f"debugpy.listen() failed or already active: {e}")
29+
30+
if not debugpy.is_client_connected():
31+
print("Waiting for client to attach debugger...")
32+
33+
def cancel_wait():
2534
time.sleep(timeout)
26-
print("Canceling debug wait task ...")
35+
print("Canceling debug wait task after timeout...")
2736
debugpy.wait_for_client.cancel()
28-
T().start()
29-
print("Waiting for client to attach debugger ...")
30-
debugpy.wait_for_client()
31-
3237

33-
if __name__ == "__main__":
34-
handler({}, {})
38+
threading.Thread(target=cancel_wait, daemon=True).start()
39+
debugpy.wait_for_client()
40+
else:
41+
print("Debugger already attached.")
3542

43+
wait_for_debug_client()
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
functions:
22
arn:aws:lambda:us-east-1:000000000000:function:function-one:
33
debug-port: 19891
4-
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
33

4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
74
# Print a message to log that this the handler of handler_function_one.py file.
85
print("The handler of handler_function_one.py is evaluating.")
96

@@ -14,25 +11,37 @@ def handler(event, context):
1411
return event
1512

1613

17-
def wait_for_debug_client(timeout=3600):
14+
def wait_for_debug_client(port: int=19891, timeout: int=3600):
1815
"""Utility function to enable debugging with Visual Studio Code"""
16+
1917
import time, threading
2018
import sys, glob
2119
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
2220
import debugpy
2321

24-
debugpy.listen(("0.0.0.0", 19891))
25-
class T(threading.Thread):
26-
daemon = True
27-
def run(self):
22+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
23+
wait_for_debug_client._debugpy_listening = False
24+
25+
if not wait_for_debug_client._debugpy_listening:
26+
try:
27+
debugpy.listen(("0.0.0.0", port))
28+
wait_for_debug_client._debugpy_listening = True
29+
print(f"debugpy is now listening on port {port}")
30+
except RuntimeError as e:
31+
print(f"debugpy.listen() failed or already active: {e}")
32+
33+
if not debugpy.is_client_connected():
34+
print("Waiting for client to attach debugger...")
35+
36+
def cancel_wait():
2837
time.sleep(timeout)
29-
print("Canceling debug wait task ...")
38+
print("Canceling debug wait task after timeout...")
3039
debugpy.wait_for_client.cancel()
31-
T().start()
32-
print("Waiting for client to attach debugger ...")
33-
debugpy.wait_for_client()
3440

41+
threading.Thread(target=cancel_wait, daemon=True).start()
42+
debugpy.wait_for_client()
43+
else:
44+
print("Debugger already attached.")
3545

36-
if __name__ == "__main__":
37-
handler({}, {})
3846

47+
wait_for_debug_client()
Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
def handler(event, context):
22
"""Lambda handler that will get invoked by the LocalStack runtime"""
33

4-
# Wait for the debugger to get attached.
5-
wait_for_debug_client()
6-
74
# Print a message to log that this the handler of handler_function_two.py file.
85
print("The handler of handler_function_two.py is evaluating.")
96

@@ -14,24 +11,35 @@ def handler(event, context):
1411
return event
1512

1613

17-
def wait_for_debug_client(timeout=3600):
18-
"""Utility function to enable debugging with Visual Studio Code"""
14+
def wait_for_debug_client(port: int=19892, timeout: int=3600):
1915
import time, threading
2016
import sys, glob
2117
sys.path.append(glob.glob(".venv/lib/python*/site-packages")[0])
2218
import debugpy
2319

24-
debugpy.listen(("0.0.0.0", 19892))
25-
class T(threading.Thread):
26-
daemon = True
27-
def run(self):
20+
if not hasattr(wait_for_debug_client, "_debugpy_listening"):
21+
wait_for_debug_client._debugpy_listening = False
22+
23+
if not wait_for_debug_client._debugpy_listening:
24+
try:
25+
debugpy.listen(("0.0.0.0", port))
26+
wait_for_debug_client._debugpy_listening = True
27+
print(f"debugpy is now listening on port {port}")
28+
except RuntimeError as e:
29+
print(f"debugpy.listen() failed or already active: {e}")
30+
31+
if not debugpy.is_client_connected():
32+
print("Waiting for client to attach debugger...")
33+
34+
def cancel_wait():
2835
time.sleep(timeout)
29-
print("Canceling debug wait task ...")
36+
print("Canceling debug wait task after timeout...")
3037
debugpy.wait_for_client.cancel()
31-
T().start()
32-
print("Waiting for client to attach debugger ...")
33-
debugpy.wait_for_client()
38+
39+
threading.Thread(target=cancel_wait, daemon=True).start()
40+
debugpy.wait_for_client()
41+
else:
42+
print("Debugger already attached.")
3443

3544

36-
if __name__ == "__main__":
37-
handler({}, {})
45+
wait_for_debug_client()

0 commit comments

Comments
 (0)