Skip to content

Commit 55b7257

Browse files
committed
examples: use with-statement
1 parent c0260fb commit 55b7257

2 files changed

Lines changed: 46 additions & 47 deletions

File tree

‎examples/chatty_client.py‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,10 @@ def xrun(userdata):
104104

105105
client.set_xrun_callback(xrun)
106106

107-
108107
print("activating JACK")
109-
client.activate()
110-
111-
print("#" * 80)
112-
print("press Return to quit")
113-
print("#" * 80)
114-
input()
115-
116-
print("closing JACK")
117-
client.close()
108+
with client:
109+
print("#" * 80)
110+
print("press Return to quit")
111+
print("#" * 80)
112+
input()
113+
print("closing JACK")

‎examples/thru_client.py‎

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,21 @@
99
acoustical feedback!
1010
1111
"""
12-
import jack
13-
import threading
1412
import sys
15-
import os
1613
import signal
14+
import os
15+
import jack
16+
import threading
17+
18+
if sys.version_info < (3, 0):
19+
# In Python 2.x, event.wait() cannot be interrupted with Ctrl+C.
20+
# Therefore, we disable the whole KeyboardInterrupt mechanism.
21+
# This will not close the JACK client properly, but at least we can
22+
# use Ctrl+C.
23+
signal.signal(signal.SIGINT, signal.SIG_DFL)
24+
else:
25+
# If you use Python 3.x, everything is fine.
26+
pass
1727

1828
argv = iter(sys.argv)
1929
# By default, use script name without extension as client name:
@@ -54,44 +64,37 @@ def shutdown(status, reason, userdata):
5464
client.inports.register("input_{0}".format(number))
5565
client.outports.register("output_{0}".format(number))
5666

57-
# Tell the JACK server that we are ready to roll.
58-
# Our process() callback will start running now.
59-
client.activate()
67+
with client:
68+
# When entering this with-statement, client.activate() is called.
69+
# This tells the JACK server that we are ready to roll.
70+
# Our process() callback will start running now.
6071

61-
# Connect the ports. You can't do this before the client is activated,
62-
# because we can't make connections to clients that aren't running.
63-
# Note the confusing (but necessary) orientation of the driver backend
64-
# ports: playback ports are "input" to the backend, and capture ports
65-
# are "output" from it.
72+
# Connect the ports. You can't do this before the client is activated,
73+
# because we can't make connections to clients that aren't running.
74+
# Note the confusing (but necessary) orientation of the driver backend
75+
# ports: playback ports are "input" to the backend, and capture ports
76+
# are "output" from it.
6677

67-
capture = client.get_ports(is_physical=True, is_output=True)
68-
if not capture:
69-
raise RuntimeError("No physical capture ports")
78+
capture = client.get_ports(is_physical=True, is_output=True)
79+
if not capture:
80+
raise RuntimeError("No physical capture ports")
7081

71-
for src, dest in zip(capture, client.inports):
72-
client.connect(src, dest)
82+
for src, dest in zip(capture, client.inports):
83+
client.connect(src, dest)
7384

74-
playback = client.get_ports(is_physical=True, is_input=True)
75-
if not playback:
76-
raise RuntimeError("No physical playback ports")
85+
playback = client.get_ports(is_physical=True, is_input=True)
86+
if not playback:
87+
raise RuntimeError("No physical playback ports")
7788

78-
for src, dest in zip(client.outports, playback):
79-
client.connect(src, dest)
89+
for src, dest in zip(client.outports, playback):
90+
client.connect(src, dest)
8091

81-
if sys.version_info < (3, 0):
82-
# In Python 2.x, event.wait() cannot be interrupted with Ctrl+C.
83-
# Therefore, we disable the whole KeyboardInterrupt mechanism.
84-
# This will not close the JACK client properly, but at least we can
85-
# use Ctrl+C.
86-
signal.signal(signal.SIGINT, signal.SIG_DFL)
87-
else:
88-
# If you use Python 3.x, everything is fine.
89-
pass
90-
91-
try:
9292
print("Press Ctrl+C to stop")
93-
event.wait()
94-
except KeyboardInterrupt:
95-
print("\nInterrupted by user")
96-
97-
client.close()
93+
try:
94+
event.wait()
95+
except KeyboardInterrupt:
96+
print("\nInterrupted by user")
97+
98+
# When the above with-statement is left (either because the end of the
99+
# code block is reached, or because an exception was raised inside),
100+
# client.deactivate() and client.close() are called automatically.

0 commit comments

Comments
 (0)