1

我想在一个无限的while循环中做一个for循环。这个无限循环从 ElasticCloud 获取 3 个不同索引的数据进行操作。我试图在第二个循环上设置条件,但它变成了无限的。

这是我正在尝试做的事情:

while(True):
  datetime_BO = datetime.now(tz_BO) #Date of today

  ed_data1 = ed.DataFrame(es, "_index_1").tail(2) #Get last data of index 1 
  ed_data2 = ed.DataFrame(es, "_index_2").tail(2) #Get last data of index 2

  panda_data1 = ed.eland_to_pandas(ed_data1) #Converting to pandas
  panda_data2 = ed.eland_to_pandas(ed_data2)
  panda_data1 = panda_data1.sort_values(by="@timestamp") #Sort by date
  panda_data2 = panda_data2.sort_values(by="@timestamp")
  panda_data1.reset_index(drop=True, inplace=True) #Index date
  panda_data2.reset_index(drop=True, inplace=True)
  panda_data1 = panda_data1.groupby(panda_data1.index // 2).mean() #Mean this measure
  panda_data2 = panda_data2.groupby(panda_data2.index // 2).mean()

  i = 5
  list_n = [] #Empty list
  n = 0
  while n < i:
    ed_data3 = ed.DataFrame(es, "heartbeat-*").tail(4) #Getting last data of this index
    panda_data3 = ed.eland_to_pandas(ed_data3) #Converting to pandas
    panda_data3 = panda_data3.sort_values(by="@timestamp") #Sorting by date
    panda_data3 = panda_data3[panda_data3['field_1'].isin(['Yes','No'])] #Getting in the column "field_1" all values with "Yes" or "No"
    panda_data3.reset_index(drop=True, inplace=True) #New index
    status = panda_data3.iloc[:,1] #Lock the "field_1" column
    if status[0] == "down" and status[1] == "No": #If in field_1 column there is a "No", give a 0
      uptime = 0
    elif status[0] == "up" or status[1] == "Yes": #If in field_1 column there is a "No", give a 1
      uptime = 1
    else:
      pass
    list_n.append(int(uptime)) #Data for empty list
    time.sleep(5) #Wait 5 secs
    n+=1
    print(list_n)

我得到的i = 10是这个

[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1]
[1, 1, 1, 1, 1, 1, 1]

在完成条件并继续获取其他数据后,我需要退出循环n = i,但它进入了一个永恒循环。

知道我在做什么吗?

编辑1:

我尝试使用break

    n += 1
    print(list_n)
  break

但是现在,无限while(true)行不通了。

4

1 回答 1

2

只需在第二个 while 结束后添加第二个中断,在第一个的缩进中

    n+=1
    print(list_n)
break
于 2021-08-03T02:54:14.787 回答