开发者

using an asymptote for program completion

This question could be answered in a language-agnostic fashion, but I am using python (fyi).

I am running an infinite loop which needs to terminate when during the last minute <10 new objects are discovered.

Eg:


while True:
    newobjs = dig_more_obj开发者_高级运维ects(obj)
    if less than 10 newobjs have been discovered over the last minute
        break

EDIT: The question is this: How do I implement this line:

if less than 10 newobjs have been discovered over the last minute


Here's a rough stab at it -- depending on the nature of dig_more_objects you may need to adjust the condition:

import time
results = []
while True:
    mark = time.time()
    newobjs = dig_more_objects(obj)
    elapsed = time.time() - mark
    results.append((newobjs, elapsed))
    count = 0
    threshhold = 0
    for objs, elapsed in results[::-1]:
        count += len(objs)  # or +1 of dig_more_objects only returns one at a time
        threshhold += elapsed
        if threshhold > 60.0 and count < 10:
            break


Use a collections.deque to hold the times of the discovered objects, and if you pop a value that is less than one minute old and there are 9 or fewer items in the deque, break out of the loop. Don't forget to push it back in if there are more than 9 items though. Items older than a minute get discarded.


You can try the multiton pattern with a timer and design such a class. See this for an idea

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜