gpt4 book ai didi

Python在streamlit中使用concurrent.futures得到 "missing ReportContext"

转载 作者:行者123 更新时间:2023-12-04 13:31:54 26 4
gpt4 key购买 nike

当我在 streamlit 应用程序中使用 concurrent.futures 时,我在控制台中收到了很多警告。
一些警告:

2020-10-28 15:43:59.338 Thread 'ThreadPoolExecutor-1_11': missing ReportContext
2020-10-28 15:43:59.338 Thread 'ThreadPoolExecutor-1_8': missing ReportContext
2020-10-28 15:43:59.339 Thread 'ThreadPoolExecutor-1_9': missing ReportContext
2020-10-28 15:43:59.339 Thread 'ThreadPoolExecutor-1_6': missing ReportContext
2020-10-28 15:43:59.339 Thread 'ThreadPoolExecutor-1_7': missing ReportContext
2020-10-28 15:43:59.340 Thread 'ThreadPoolExecutor-1_10': missing ReportContext
2020-10-28 15:43:59.340 Thread 'ThreadPoolExecutor-1_11': missing ReportContext
2020-10-28 15:43:59.341 Thread 'ThreadPoolExecutor-1_8': missing ReportContext
2020-10-28 15:43:59.341 Thread 'ThreadPoolExecutor-1_9': missing ReportContext
2020-10-28 15:43:59.342 Thread 'ThreadPoolExecutor-1_10': missing ReportContext
2020-10-28 15:43:59.342 Thread 'ThreadPoolExecutor-1_11': missing ReportContext
我知道它应该使用 add_report_ctx(thread) 来创建线程。 ( reference here )
我想知道如何在concurrent.futures中使用?
这是我的代码的一部分:
def thread_run(func, values):
with ThreadPoolExecutor(max_workers=60) as executor:
futures = [executor.submit(func, value) for value in values]
for future in as_completed(futures):
yield future.result()

最佳答案

您收到此警告是因为您正尝试将小部件等添加到来自另一个线程的流媒体页面,并且它不知道在此处发送它们。最好不要这样做,因为该函数将异步运行,您可能无法按所需顺序获取小部件等。更好的解决方案是在函数中进行任何计算,返回结果并添加小部件等。在主流线。
但是,如果您确实希望这样做,您可以将报告上下文添加到将运行的函数中的当前线程

def func(value, ctx):
add_report_ctx(threading.currentThread(), ctx)
# rest of func
并从您的 thread_run 函数中获取上下文(假设它在您的主流线程上运行):
def thread_run(func, values):
with ThreadPoolExecutor(max_workers=60) as executor:
ctx = st.report_thread.get_report_ctx()
futures = [executor.submit(func, value, ctx) for value in values]
for future in as_completed(futures):
yield future.result()
否则,从任何地方收集它们并通过 thread_run 函数传递它们:
def thread_run(func, values, ctxs):
with ThreadPoolExecutor(max_workers=60) as executor:
futures = [executor.submit(func, value, ctx) for value, ctx in zip(values, ctxs)]
for future in as_completed(futures):
yield future.result()

关于Python在streamlit中使用concurrent.futures得到 "missing ReportContext",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64568709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com