停止主线程直到ThreadPoolExecutor中的所有任务完成 – Python DJANGO

huangapple go评论62阅读模式
英文:

Stop the main thread until all task done in a ThreadPoolExecutor - Python DJANGO

问题

以下是您提供的代码的中文翻译部分:

from concurrent.futures import ThreadPoolExecutor

def resultados(request):
    executor = ThreadPoolExecutor() 
    context = {}
    executor.submit(resultados_proceso, regiones, territorios, canales, marcas, periodos)
    executor.submit(resultadosGrafica_proceso,regiones, territorios, canales, marcas, periodos)
    executor.submit(get_volumen_ejecucion_clientes_iniciativa, 106)
    executor.submit(get_volumenAP_xmarca, ['M'])

return render(request, 'resultados.html', context)

def resultados_proceso(context, regiones, territorios, canales, marcas, periodos):
    context['resultados'] = getResultados(regiones, territorios, canales, marcas, periodos)

def resultadosGrafica_proceso(context, regiones, territorios, canales, marcas, periodos):
    context['grafica'] = getResultadosGrafica(regiones, territorios, canales, marcas, periodos)

希望这对您有所帮助。如果您需要任何进一步的帮助,请随时提问。

英文:

I've some rather heavy stored procedures in a view and I wanted to use threads to execute those queries in parallel but I don't know how to stop the main thread until those queries have finished.

My code:

from concurrent.futures import ThreadPoolExecutor

def resultados(request):
    executor = ThreadPoolExecutor() 
    context = {}
    executor.submit(resultados_proceso, regiones, territorios, canales, marcas, periodos)
    executor.submit(resultadosGrafica_proceso,regiones, territorios, canales, marcas, periodos)
    executor.submit(get_volumen_ejecucion_clientes_iniciativa, 106)
    executor.submit(get_volumenAP_xmarca, ['M'])

return render(request, 'resultados.html', context)

def resultados_proceso(context, regiones, territorios, canales, marcas, periodos):
    context['resultados'] = getResultados(regiones, territorios, canales, marcas, periodos)

def resultadosGrafica_proceso(context, regiones, territorios, canales, marcas, periodos):
    context['grafica'] = getResultadosGrafica(regiones, territorios, canales, marcas, periodos)

答案1

得分: 1

你是否指的是当前请求-响应线程?

如果我没记错的话,你可以调用 'result' 方法。

所以,

with concurrent.futures.ThreadPoolExecutor() as executor:
    future1 = executor.submit(do_something, 1.7)
    future2 = executor.submit(do_something_2, 1.7)

    future1.result()
    future2.result()  # 会阻塞当前线程

希望这能有所帮助。

附言 -
我也尝试过探索一些异步库,可以等待结果而不阻塞。

英文:

did you meant the current request-response thread ?

IIRC, you can call the 'result' method.

So,

with concurrent.futures.ThreadPoolExecutor() as executor:
    future1 = executor.submit(do_something, 1.7)
    future2 = executor.submit(do_something_2, 1.7)

    future1.result()
    future2.result()  # will block current thread
    

Hopes it help a bit.

P.S -
i will also tried to explore about the async libraries which can wait to a result without blocking.

huangapple
  • 本文由 发表于 2023年5月11日 02:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定