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

标签 python concurrent.futures streamlit

当我在 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/

相关文章:

python - 高效的概率树分支

python - 在类中实例化线程

python - 空数据错误 : No columns to parse from file about streamlit

python - docker 中的 Nginx、fastapi 和 streamlit - 反向代理不适用于 streamlit

java - 仅使用一个列表就可以实现所有可能的单词排列

Python PIL ImageTk.PhotoImage() 给我一个总线错误?

python - 使用 iris 示例加载 csv 时 tensorflow 的值错误

python - 是我一个人还是 Windows 上新的 Python future 模块出现严重问题

Python: concurrent.futures 如何让它可取消?

azure - 如何通过 Azure 容器应用程序部署 Streamlit 应用程序?