主函数while循环中第一行“with concurrent.futures...”的ThreadPoolExecutor Attribute error: __enter__
出现问题。这是我第一次尝试这种线程化方式,所以我不确定问题出在哪里。
#Sets up to 25 sectors as R or Y
def crop_dehydration(plot):
for _ in range(25):
x = random.randint(0,9)
y = random.randint(0,9)
if plot[x][y] != 'G':
plot[x][y].data = random.choice(hydration_choices)
#create irrigation logic
#Scans farm for R and Y sectors to add them to independent
#shceduled irrigation lists
def scan_farm(plot):
for i in range(10):
for j in range(10):
if plot[i][j] == 'R':
R.append(plot[i][j])
if plot[i][j] == 'Y':
Y.append(plot[i][j])
#handler for R and Y groups
def irrigate_sector(group, secs):
#irrigation handler for sectors within R and Y groups
def irrigate(sector, secs):
time.sleep(secs)
sector = 'G'
return sector
with concurrent.futures.ThreadPoolExecutor as executor:
results = {executor.submit(irrigate,sector, secs) for sector in group}
for i in concurrent.futures.as_completed(results):
return i.result()
def main():
farm = Sector()
while(True):
farm.display()
crop_dehydration(farm.plot)
scan_farm(farm.plot)
with concurrent.futures.ThreadPoolExecutor as executor:
r_thread = executor.submit(irrigate_sector, R, 25)
return r_thread.results()
with concurrent.futures.ThreadPoolExecutor as executor:
y_thread = executor.submit(irrigate_sector, Y, 10)
return y_thread.results()
main()
#create app initializer
#display gui
1条答案
按热度按时间gtlvzcf81#
正如@flakes在评论中所说,问题在于这一行:
缺少调用ThreadPoolExecutor构造函数所需的
()
。该行应为: