for i, j, k, l in product(range(10, 21), range(5, 11), range(2, 11), range(1, 3)): data["normalSpeed"] = i / 3.6 data["minTurningRadius"] = j data["breadth"] = k data["jobType"] = l for block_type in ["小地块", "中地块", "大地块"]: if block_type == "小地块": data["map"]["outerbd"] = outerbd_small data["map"]["entrance"] = entrance_small data["map"]["exit"] = exit_small elif block_type == "中地块": data["map"]["outerbd"] = outerbd_middle data["map"]["entrance"] = entrance_middle data["map"]["exit"] = exit_middle else: data["map"]["outerbd"] = outerbd_large data["map"]["entrance"] = entrance_large data["map"]["exit"] = exit_large json_str = json.dumps(data) res = requests.post(url, json_str).text filename = os.path.join(desktop_path_file, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, {block_type}.txt') with open(filename, 'w') as file: file.write(res) print(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},{block_type}') logger.info(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},{block_type}') time.sleep(10) plot_route_from_file(filename, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, {block_type}') 在功能不变的情况下优化,只能用requests发送post请求
时间: 2024-04-15 21:27:05 浏览: 120
下面是经过优化的代码,使用了单个 POST 请求来处理大地块和小地块的情况:
```python
for i, j, k, l in product(range(10, 21), range(5, 11), range(2, 11), range(1, 3)):
for block_type in ["小地块", "中地块", "大地块"]:
if block_type == "小地块":
data["map"]["outerbd"] = outerbd_small
data["map"]["entrance"] = entrance_small
data["map"]["exit"] = exit_small
elif block_type == "中地块":
data["map"]["outerbd"] = outerbd_middle
data["map"]["entrance"] = entrance_middle
data["map"]["exit"] = exit_middle
else:
data["map"]["outerbd"] = outerbd_large
data["map"]["entrance"] = entrance_large
data["map"]["exit"] = exit_large
json_str = json.dumps(data)
res = requests.post(url, json_str).text
filename = os.path.join(desktop_path_file,
f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, {block_type}.txt')
with open(filename, 'w') as file:
file.write(res)
print(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},{block_type}')
logger.info(f'写入成功,速度为{i},转弯半径为{j},幅宽为{k},作业模式为{l},{block_type}')
time.sleep(10)
plot_route_from_file(filename, f'normalSpeed={i}, minTurningRadius={j}, breadth={k}, jobType={l}, {block_type}')
```
这样优化后,代码仍然会遍历所有的参数组合,并在每个组合下处理大地块和小地块。但是现在使用了单个 POST 请求来发送数据,并在每个循环迭代中进行处理。这样可以提高代码的效率。
阅读全文