Skip to content

Commit 4caf662

Browse files
committed
Make scheduling delays more idiomatic.
1 parent 40c4538 commit 4caf662

1 file changed

Lines changed: 18 additions & 49 deletions

File tree

bin/sendAlertStream.py

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,14 @@
2525
content.
2626
"""
2727

28-
from __future__ import print_function
2928
import argparse
29+
import asyncio
3030
import glob
31+
import itertools
3132
import time
32-
import asyncio
3333
from lsst.alert.stream import alertProducer
3434
from lsst.alert.packet import retrieve_alerts
3535

36-
37-
@asyncio.coroutine
38-
def delay(wait_sec, function, *args):
39-
"""Sleep for a given time before calling a function.
40-
Parameters
41-
----------
42-
wait_sec
43-
Time in seconds to sleep before calling `function`.
44-
function
45-
Function to return after sleeping.
46-
"""
47-
yield from asyncio.sleep(wait_sec)
48-
return function(*args)
49-
50-
51-
@asyncio.coroutine
52-
def schedule_delays(eventloop, function, argslist, interval=39):
53-
"""Schedule delayed calls of functions at a repeating interval.
54-
Parameters
55-
----------
56-
eventloop
57-
Event loop returned by asyncio.get_event_loop().
58-
function
59-
Function to be scheduled.
60-
argslist
61-
List of inputs for function to loop over.
62-
interval
63-
Time in seconds between calls.
64-
"""
65-
counter = 1
66-
for arg in argslist:
67-
wait_time = interval - (time.time() % interval)
68-
yield from asyncio.ensure_future(delay(wait_time, function, arg))
69-
print('visits finished: {} \t time: {}'.format(counter, time.time()))
70-
counter += 1
71-
eventloop.stop()
72-
73-
7436
def main():
7537
parser = argparse.ArgumentParser(description=__doc__)
7638
parser.add_argument('broker', type=str,
@@ -89,23 +51,30 @@ def main():
8951
files.sort()
9052

9153
def send_visit(f):
92-
print('visit:', f[15:20], '\ttime:', time.time())
54+
start_time = time.time()
55+
print('visit:', f[15:20], '\ttime:', start_time)
9356
# Load alert contents
9457
with open(f, mode='rb') as file_data:
9558
# TODO replace Avro files with visits having better S/N cut
9659
# for now, limit to first 10,000 alerts (current have ~70,000)
9760
schema, alert_packets = retrieve_alerts(file_data)
98-
alert_count = 0
99-
for record in alert_packets:
100-
if alert_count < 10000:
101-
streamProducer.send(schema, record)
102-
alert_count += 1
103-
else:
104-
break
61+
ALERTS_TO_SEND = 10000
62+
for alert_count, record in enumerate(alert_packets):
63+
if alert_count < ALERTS_TO_SEND:
64+
streamProducer.send(schema, record)
65+
else:
66+
break
10567
streamProducer.flush()
68+
print(f"Sent {alert_count} alerts in {time.time() - start_time}s.")
10669

70+
# Schedule visits to be send every `interval` seconds.
10771
loop = asyncio.get_event_loop()
108-
asyncio.ensure_future(schedule_delays(loop, send_visit, files))
72+
interval = 39 # Seconds between visits
73+
for delay, filename in zip(itertools.count(0, interval), files):
74+
loop.call_later(delay, send_visit, filename)
75+
76+
# Shut down the event loop after the last visit has been sent.
77+
loop.call_later(delay, loop.stop)
10978
loop.run_forever()
11079
loop.close()
11180

0 commit comments

Comments
 (0)