本文整理汇总了Python中datetime.timedelta.is_timed_out方法的典型用法代码示例。如果您正苦于以下问题:Python timedelta.is_timed_out方法的具体用法?Python timedelta.is_timed_out怎么用?Python timedelta.is_timed_out使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类datetime.timedelta
的用法示例。
在下文中一共展示了timedelta.is_timed_out方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: improve_implementation
# 需要导入模块: from datetime import timedelta [as 别名]
# 或者: from datetime.timedelta import is_timed_out [as 别名]
def improve_implementation(
impl : Implementation,
timeout : datetime.timedelta = datetime.timedelta(seconds=60),
progress_callback : Callable[[Implementation], Any] = None,
improve_count : Value = None) -> Implementation:
"""Improve an implementation.
This function tries to synthesize a better version of the given
implementation. It returns the best version found within the given timeout.
If provided, progress_callback will be called whenever a better
implementation is found. It will be given the better implementation, which
it should not modify or cache.
"""
start_time = datetime.datetime.now()
# we statefully modify `impl`, so let's make a defensive copy which we will modify instead
impl = impl.safe_copy()
# worker threads ("jobs"), one per query
improvement_jobs = []
with jobs.SafeQueue() as solutions_q:
def stop_jobs(js):
"""Stop the given jobs and remove them from `improvement_jobs`."""
js = list(js)
jobs.stop_jobs(js)
for j in js:
improvement_jobs.remove(j)
def reconcile_jobs():
"""Sync up the current set of jobs and the set of queries.
This function spawns new jobs for new queries and cleans up old
jobs whose queries have been dead-code-eliminated."""
# figure out what new jobs we need
job_query_names = set(j.q.name for j in improvement_jobs)
new = []
for q in impl.query_specs:
if q.name not in job_query_names:
states_maintained_by_q = impl.states_maintained_by(q)
new.append(ImproveQueryJob(
impl.abstract_state,
list(impl.spec.assumptions) + list(q.assumptions),
q,
context=impl.context_for_method(q),
k=(lambda q: lambda new_rep, new_ret: solutions_q.put((q, new_rep, new_ret)))(q),
hints=[EStateVar(c).with_type(c.type) for c in impl.concretization_functions.values()],
freebies=[e for (v, e) in impl.concretization_functions.items() if EVar(v) in states_maintained_by_q],
ops=impl.op_specs,
improve_count=improve_count))
# figure out what old jobs we can stop
impl_query_names = set(q.name for q in impl.query_specs)
old = [j for j in improvement_jobs if j.q.name not in impl_query_names]
# make it so
stop_jobs(old)
for j in new:
j.start()
improvement_jobs.extend(new)
# start jobs
reconcile_jobs()
# wait for results
timeout = Timeout(timeout)
done = False
while not done and not timeout.is_timed_out():
for j in improvement_jobs:
if j.done:
if j.successful:
j.join()
else:
print("failed job: {}".format(j), file=sys.stderr)
# raise Exception("failed job: {}".format(j))
done = all(j.done for j in improvement_jobs)
try:
# list of (Query, new_rep, new_ret) objects
results = solutions_q.drain(block=True, timeout=0.5)
except Empty:
continue
# group by query name, favoring later (i.e. better) solutions
print("updating with {} new solutions".format(len(results)))
improved_queries_by_name = OrderedDict()
killed = 0
for r in results:
q, new_rep, new_ret = r
if q.name in improved_queries_by_name:
killed += 1
improved_queries_by_name[q.name] = r
if killed:
print(" --> dropped {} worse solutions".format(killed))
#.........这里部分代码省略.........