Skip to content

Commit aa9019a

Browse files
committed
Improve callgen speed
When the sound directory is large, iterating over all items appears to be really slow. For this reason, we change the behavior from searching all subdirs to explicitly mention search directories in the commandline. Only these paths will be searched for sound files. No subdirectories anymore. This eliminates the need for iteration by only testing if the searched file exists.
1 parent fd9ddc7 commit aa9019a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

yate/callgen.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
import signal
55
import logging
6+
from pathlib import Path
67

78
from aiohttp import web
89

@@ -11,7 +12,6 @@
1112

1213
soundfile_extensions = [".slin", ".gsm"]
1314

14-
logging.basicConfig(level=logging.INFO)
1515

1616

1717
class SoundCallInfo:
@@ -30,7 +30,7 @@ def __init__(self, port, sounds_directory, bind_global=False):
3030
self.active_calls = {}
3131
self.yate = YateAsync("127.0.0.1", port)
3232
self.yate.set_termination_handler(self.termination_handler)
33-
self.sounds_directory = sounds_directory
33+
self.sounds_directories = sounds_directory
3434

3535
self.web_app = web.Application()
3636
self.web_app.add_routes([web.post("/call", self.web_call_handler)])
@@ -79,6 +79,7 @@ def termination_handler():
7979

8080

8181
async def web_call_handler(self, request):
82+
logging.debug("TRACE: Request handler begin")
8283
params = await request.post()
8384

8485
soundfile = params.get("soundfile")
@@ -167,20 +168,26 @@ def drop_call_if_not_answered(self, id):
167168
self._drop_call(id)
168169

169170
def find_soundfile(self, name):
170-
for root, _, files in os.walk(self.sounds_directory):
171-
for f in files:
172-
fname, fext = os.path.splitext(f)
173-
if fname == name and fext in soundfile_extensions:
174-
return os.path.join(root, f)
175-
171+
for directory in self.sounds_directories:
172+
for ext in soundfile_extensions:
173+
test_path = Path(directory) / (name + ext)
174+
logging.debug("Testing %s for existence", test_path)
175+
if test_path.exists():
176+
return str(test_path)
176177

177178
def main():
178179
parser = argparse.ArgumentParser(description='Yate CLI to generate automated calls.')
179180
parser.add_argument("port", type=int, help="The port at which yate is listening")
180-
parser.add_argument("sounds_directory", type=str, help="The directory at which we find the sounds")
181+
parser.add_argument("sounds_directory", type=str, nargs="+", help="Directories at which we find the sounds")
181182
parser.add_argument("--bind_global", action="store_true")
183+
parser.add_argument("--trace", action="store_true", help="Enable debug tracing")
184+
182185

183186
args = parser.parse_args()
187+
if args.trace:
188+
logging.basicConfig(level=logging.DEBUG)
189+
else:
190+
logging.basicConfig(level=logging.INFO)
184191
app = YateCallGenerator(args.port, args.sounds_directory, args.bind_global)
185192
app.run()
186193

0 commit comments

Comments
 (0)