Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为循环并行化?_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch_Parallel Processing - Fatal编程技术网 elasticsearch,parallel-processing,Python,elasticsearch,Parallel Processing" /> elasticsearch,parallel-processing,Python,elasticsearch,Parallel Processing" />

Python 如何为循环并行化?

Python 如何为循环并行化?,python,elasticsearch,parallel-processing,Python,elasticsearch,Parallel Processing,我正在做一个项目,我有多个查询来从Elasticsearch中提取数据 我想在Elasticsearch中同时并在很短的时间内(实时用例)从两个不同的索引进行查询。 我应该做些什么来提高循环时间,并将我的两个for循环并行化。 这是我代码的一部分: t2 = time.time() for i in range(docs_enter): enter_t = req_enter['hits']['hits'][i]['_source']['Time'] enter_time =

我正在做一个项目,我有多个查询来从Elasticsearch中提取数据

我想在Elasticsearch中同时并在很短的时间内(实时用例)从两个不同的索引进行查询。 我应该做些什么来提高循环时间,并将我的两个
for
循环并行化。 这是我代码的一部分:

t2 = time.time()

for i in range(docs_enter):
    enter_t = req_enter['hits']['hits'][i]['_source']['Time']
    enter_time = datetime.strptime(enter_t,time_format)
    total_duration = now - enter_time
    if total_duration < threshold_hours:
        enter_face = req_enter['hits']['hits'][i]['_source']['FaceID']
        enter_img = np.array(enter_face).astype(np.uint8)
        match_enter = verifyFace(crop, enter_img)
        acc_enter.append(match_enter)

t3 = time.time()

for j in range(docs_guichet):
    guichet_t = req_guichet['hits']['hits'][j]['_source']['Time']
    guichet_time = datetime.strptime(guichet_t,time_format)
    total_duration = now - guichet_time
    if total_duration < threshold_hours:
        guichet_face = req_guichet['hits']['hits'][j]['_source']['FaceID']
        guichet_img = np.array(guichet_face).astype(np.uint8)
        match_guichet = verifyFace(crop, guichet_img)
        acc_guichet.append(match_guichet)

t4 = time.time()

print(f"\n------ 1st loop time {t3 - t2} -------\n")
print(f"\n------ 2nd loop time {t4 - t3} -------\n")
t2=time.time()
对于范围内的i(输入文档):
输入请求输入['hits']['hits'][i]['u source']['Time']
输入时间=datetime.strtime(输入时间格式)
总持续时间=现在-输入持续时间
如果总持续时间<阈值小时:
输入[u face=req][u输入['hits']['hits'][i]['u source']['FaceID']
输入\ img=np.array(输入\ face).astype(np.uint8)
匹配输入=验证面(裁剪,输入图像)
acc\u enter.追加(匹配\u enter)
t3=时间。时间()
对于范围内的j(docs_guichet):
guichet\u t=req\u guichet['hits']['hits'][j]['u source']['Time']
guichet_time=datetime.strtime(guichet_t,time_格式)
总持续时间=现在-guichet\u时间
如果总持续时间<阈值小时:
guichet_face=req_guichet['hits']['hits'][j]['u source']['FaceID']
guichet_img=np.array(guichet_face).astype(np.uint8)
match_guichet=验证面(裁剪、guichet_img)
acc_guichet.append(匹配_guichet)
t4=时间。时间()
打印(f“\n------第一次循环时间{t3-t2}------\n”)
打印(f“\n------第二次循环时间{t4-t3}------\n”)
这是这两个循环的运行时间:

我应该怎样做才能:

  • 提高循环时间
  • 将两个for循环并行化

  • 你可以使用多线程。@goon我已经尝试过了,但是没有任何改变!那么也许你没有正确地实现它。