Python在streamlit中使用concurrent.futures获得“缺少ReportContext”

2024-09-29 23:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

当我在Streamlight应用程序中使用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(线程)来创建线程。(reference here

我想知道如何在期货交易中使用

以下是我的部分代码:

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()

Tags: in警告forvalueasfuture线程thread
1条回答
网友
1楼 · 发布于 2024-09-29 23:18:59

您收到此警告是因为您正试图从另一个线程向Streamlight页面添加小部件等,而它不知道如何在此处发送它们。最好不要这样做,因为函数将异步运行,并且您可能无法按所需顺序获取小部件等。更好的解决方案是在函数中执行任何计算,返回结果并在Streamlight主线程中添加小部件等

但是,如果确实希望这样做,可以将报表上下文添加到将要运行的函数中的当前线程

def func(value, ctx):
    add_report_ctx(threading.currentThread(), ctx)
    # rest of func

并从thread_run函数中获取上下文(假设该函数在您的streamlight主线程上运行):

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()

相关问题 更多 >

    热门问题