Python 如何确保while在各个方面都会停止
我正在为KODI编写一个脚本,当出现代码错误时,我发现了一个无限循环。这个循环意味着我必须登录到另一个帐户或重新启动计算机才能停止KODI 因此,我的问题是,如何确保Python 如何确保while在各个方面都会停止,python,xbmc,kodi,Python,Xbmc,Kodi,我正在为KODI编写一个脚本,当出现代码错误时,我发现了一个无限循环。这个循环意味着我必须登录到另一个帐户或重新启动计算机才能停止KODI 因此,我的问题是,如何确保而将在各个方面停止 这只是脚本的一部分,代码被包装在try中。您还需要知道,如果我出现错误,try始终有效 代码中的问题是while部分,例如atp.get()。这会导致一个错误,因为我在调用get()之前没有检查工作是否已完成(为了您的缘故,我没有更正此错误) 问题是,即使我使用了try,一个意外错误也会导致而无法停止! 更
而将在各个方面停止
这只是脚本的一部分,代码被包装在try
中。您还需要知道,如果我出现错误,try
始终有效
代码中的问题是while
部分,例如atp.get()
。这会导致一个错误,因为我在调用get()
之前没有检查工作是否已完成(为了您的缘故,我没有更正此错误)
问题是,即使我使用了try
,一个意外错误也会导致而无法停止!
更新
我不确定while
中写入的代码是否只会影响while
过程,或者是否有其他方面会影响while
过程
更新2
查看get()
是否返回错误的测试。使用Python3.x进行测试,而不是KODI使用的Python2.x
更新3
查看get()
是否返回raise错误的测试。使用Python3.4.2和Python2.7.10进行测试。
更新4
问题仍然是:
while
是否仅受while
中写入的代码的影响,或者其他方面是否会影响while
过程
- 如何确保
while
将在各个方面停止
更新5
似乎没有一种解决方案可以使while
在各个方面都停止。
因此,我认为简单的解决方案是在while
之后检查池。
错误不是发生在get()
,而是发生在apply\u async()
中的target。因此,这种解决方案只能使该过程再次停止
结论是。尽管池
有故障,循环仍将继续,因此您可以随时退出循环。
此外,对于Python 3.4.2和2.7.10,需要注意的是,Pool
只会在完成时引发一个错误,任何额外的get()
都可以用来检查结果是否有错误。从您使用Pool
我猜您使用的是多处理。如果子进程引发异常,我认为这不会导致主进程停止执行,因此while循环继续运行。@Kevin Yes,我正在使用多处理:)但Yes池将继续运行,直到它完成为止,但是如果发生了引发异常,get()
将返回错误。您确定get
将引发错误吗?那里的p
是什么类型的?p是一个池:)我会给你发一个代码示例。。。哦,我忘了我测试了这段代码,这是Python3.x,而不是KODI使用的Python2.x…@Kevin,它适用于Python2.7.1
def browse(separate, page):
[...]
# Getting meta data and subtitles
pools = {'metadata': [], 'subtitles': []}
with closing(multiprocessing.Pool(processes=2)) as pool:
provider_meta = call_provider(PROVIDERS['meta_tmdb'])
provider_subtitle = call_provider(PROVIDERS['subtitle_subscene'])
for item in items:
pools['metadata'].append(pool.apply_async(provider_meta.get, args=(item["info"]["code"], item["label"], item["info"]["year"]), callback=pool_stats))
pools['subtitles'].append(pool.apply_async(provider_subtitle.get, args=(item["info"]["code"], item["label"], item["info"]["year"]), callback=pool_stats))
pool_checklist = _create_checklist(pools)
while not xbmc.abortRequested and not dialog.iscanceled():
xbmc.sleep(100)
# Check if are raise a error
for p in pool_checklist:
p.get()
# Break when all requests are done
if all(p.ready() for p in pool_checklist):
break
else:
return
[...]
def _create_checklist(pools):
plist = []
for c in pools.values():
for p in c:
plist.append(p)
return plist
from multiprocessing import Pool
from contextlib import closing
import time
def func(x, i):
if i == 10:
raise Exception('ttt')
return {'x':i}
def go():
try:
def callback(x):
print('done: '+str(x['x']))
pools = []
with closing(Pool(processes=2)) as pool:
for i in range(20):
pools.append(pool.apply_async(func, args=(i,i), callback=callback))
while not all(p.ready() for p in pools):
time.sleep(1)
list = map(lambda p: p.get(), pools)
for l in list:
print(l)
print('Finished with the script')
except:
print('ERROR')
if __name__ == '__main__':
go()
def browse(separate, page):
[...]
# Getting meta data and subtitles
pools = {'metadata': [], 'subtitles': []}
with closing(multiprocessing.Pool(processes=2)) as pool:
provider_meta = call_provider(PROVIDERS['meta_tmdb'])
provider_subtitle = call_provider(PROVIDERS['subtitle_subscene'])
for item in items:
pools['metadata'].append(pool.apply_async(provider_meta.get, args=(item["info"]["code"], item["label"], item["info"]["year"]), callback=pool_stats))
pools['subtitles'].append(pool.apply_async(provider_subtitle.get, args=(item["info"]["code"], item["label"], item["info"]["year"]), callback=pool_stats))
pool_checklist = _create_checklist(pools)
while not all(p.ready() for p in pool_checklist):
if xbmc.abortRequested or dialog.iscanceled()
return
xbmc.sleep(100)
# Check the pools for errors
for p in pool_checklist:
p.get()
[...]
def _create_checklist(pools):
plist = []
for c in pools.values():
for p in c:
plist.append(p)
return plist
def browse(separate, page):
[...]
# Getting meta data and subtitles
pools = {'metadata': [], 'subtitles': []}
with closing(multiprocessing.Pool(processes=2)) as pool:
provider_meta = call_provider(PROVIDERS['meta_tmdb'])
provider_subtitle = call_provider(PROVIDERS['subtitle_subscene'])
for item in items:
pools['metadata'].append(pool.apply_async(provider_meta.get, args=(item["info"]["code"], item["label"], item["info"]["year"]), callback=pool_stats))
pools['subtitles'].append(pool.apply_async(provider_subtitle.get, args=(item["info"]["code"], item["label"], item["info"]["year"]), callback=pool_stats))
pool_checklist = _create_checklist(pools)
# Loop
while not all(p.ready() for p in pool_checklist):
if xbmc.abortRequested or dialog.iscanceled():
return
xbmc.sleep(100)
# Check the results for errors
for p in pool_checklist:
p.get()
[...]