怎么处理return values:-1. error 115:Operation now in progress
时间: 2023-12-21 08:04:28 浏览: 26
当在使用非阻塞的套接字操作时,如果值是-1且错误代码为115(EINPROGRESS),表示操作正在进行中。在这种况下,你可以采取以下步骤来:
1. 使用`select()`或`poll()`函数来等待套接字的可写性。这将告诉你套字何时可写,即操作何时完成2. 如果套接字可写,则表示操作完成。你可以继续进行后续的操作。
3. 如果套接字不可写,并且你希望等待操作完成,可以使用循环来重复调用`select()`或`poll()`函数,直到套接字可写为止。
4. 如果你不想等待操作完成,并且希望在操作进行中时执行其他任务,可以采取适当的操作,如动其他线程或处理其他任务。
总之,处理这个返回值和错误代码的关键是使用适当的等待机制,以确保在操作完成后再进行后续的操作。
相关问题
Input In [9] def data= df.iloc[:, 1:-2].values ^ SyntaxError: invalid syntax
这错误提示是因为在 Python 中,我们不能使用等号 `=` 来定义函数的参数。相反,你需要使用冒号 `:` 来指定参数类型和箭头 `->` 来指定返回值类型。这是一个示例:
```python
def my_function(my_parameter: str) -> int:
# 在这里编写函数体
return 42
```
所以,对于你的代码,你需要将 `=` 换成 `:` ,就像这样:
```python
def data(df: pd.DataFrame) -> np.ndarray:
return df.iloc[:, 1:-2].values
```
这应该可以解决你遇到的 `SyntaxError` 错误。
优化代码 var modelMap = map[string]interface{}{ "comb": &models.PsComb{}, "conduit": &models.PsConduit{}, "feature": &models.Feature{}, "discharger": &models.PsDischarger{}, "gate": &models.PsGate{}, "interception": &models.PsInterception{}, "lake": &models.PsLake{}, "manhole": &models.PsManhole{}, "outfall": &models.PsOutfall{}, "pipe": &models.PsPipe{}, "pumpStation": &models.PsPumpStation{}, "retention": &models.PsRetention{}, "river": &models.PsRiver{}, "wwtp": &models.PsWWTP{}, "psSystem": &models.PsSystem{}, "landUse": &models.LandUse{}, "publicity": &models.Publicity{}, } func Batch(ctx *gin.Context) { var requ Request if err := ctx.ShouldBind(&requ); err != nil { ctx.JSON(400, models.Response{ Code: 400, Msg: "参数有误", Data: err.Error(), }) return } resp := models.Response{ Code: 0, Msg: "success", } for _, item := range requ.Post { if reqType, ok := modelMap[item.Type]; ok { if err := mapstructure.Decode(item.Value, reqType); err != nil { panic(err) } res := common.DB.Create(reqType) if res.Error != nil { resp.Code = 500 resp.Msg = "创建失败" resp.Data = res.Error ctx.JSON(500, resp) return } ctx.JSON(200, resp)
这段代码看起来比较简单,但可以从以下几个方面进行优化:
1. 将 `modelMap` 改为在 `init()` 函数中初始化,而不是在每次调用 `Batch()` 函数时初始化。这样可以避免重复初始化,提高代码的性能。
2. 针对循环中的每个 `item`,可以采用批量插入(bulk insert)的方式,而不是逐个插入。这样可以减少数据库交互的次数,提高代码的性能。例如,可以将所有待插入的数据构造成一个切片,然后通过 `common.DB.CreateInBatches()` 方法进行批量插入。
3. 当出现错误时,应该立即返回错误信息,而不是继续进行循环。这样可以避免不必要的数据库交互和数据错误。
下面是一个优化后的示例代码:
```
var modelMap = map[string]interface{}{
"comb": &models.PsComb{},
"conduit": &models.PsConduit{},
"feature": &models.Feature{},
"discharger": &models.PsDischarger{},
"gate": &models.PsGate{},
"interception": &models.PsInterception{},
"lake": &models.PsLake{},
"manhole": &models.PsManhole{},
"outfall": &models.PsOutfall{},
"pipe": &models.PsPipe{},
"pumpStation": &models.PsPumpStation{},
"retention": &models.PsRetention{},
"river": &models.PsRiver{},
"wwtp": &models.PsWWTP{},
"psSystem": &models.PsSystem{},
"landUse": &models.LandUse{},
"publicity": &models.Publicity{},
}
func init() {
for _, model := range modelMap {
common.DB.AutoMigrate(model)
}
}
func Batch(ctx *gin.Context) {
var requ Request
if err := ctx.ShouldBind(&requ); err != nil {
ctx.JSON(400, models.Response{
Code: 400,
Msg: "参数有误",
Data: err.Error(),
})
return
}
resp := models.Response{
Code: 0,
Msg: "success",
}
var values []interface{}
for _, item := range requ.Post {
if reqType, ok := modelMap[item.Type]; ok {
if err := mapstructure.Decode(item.Value, reqType); err != nil {
ctx.JSON(500, models.Response{
Code: 500,
Msg: "创建失败",
Data: err.Error(),
})
return
}
values = append(values, reqType)
}
}
if len(values) > 0 {
res := common.DB.CreateInBatches(values, len(values))
if res.Error != nil {
ctx.JSON(500, models.Response{
Code: 500,
Msg: "创建失败",
Data: res.Error,
})
return
}
}
ctx.JSON(200, resp)
}
```
注意,这只是一种示例代码,具体实现可能需要根据您的实际情况进行调整。