小编vas*_*man的帖子

睡眠的行为和选择在去

我试图更多地了解在Go中各种阻塞/等待操作类型期间在表面下发生的事情.请看以下示例:

otherChan = make(chan int)
t = time.NewTicker(time.Second)
for {
    doThings()

    // OPTION A: Sleep
    time.Sleep(time.Second)

    // OPTION B: Blocking ticker
    <- t.C

    // OPTION C: Select multiple
    select {
        case <- otherChan:
        case <- t.C:
    }
}
Run Code Online (Sandbox Code Playgroud)

从低级别的视图(系统调用,cpu调度)这些等待时间有什么区别?

我的理解是time.Sleep让CPU自由执行其他任务,直到指定的时间结束.阻止股票代码是否<- t.C也这样做?处理器是否轮询通道或是否涉及中断?选择中有多个频道会改变什么吗?

换句话说,假设otherChan从未有任何东西投入其中,这三个选项是否会以相同的方式执行,还是会比其他选项的资源密集程度更低?

resources go blocking

11
推荐指数
1
解决办法
2037
查看次数

Python:启动 psql 查询,不要等待响应

我正在使用 python 和 psycopg2 开始一个 COPY TO CSV,这将需要很长时间(可能是几个小时)。复制到文件工作将由 postgres 处理,因此不需要将任何信息返回给我的 python 脚本。

有没有办法让我将查询传递给 postgres,然后在不等待响应的情况下断开连接,以便我的程序可以处理其他任务?

这是启动作业的方法:

def startJob(self):
    #This bit will take the information and flags from the file and start the psql job
    conn = psycopg2.connect('dbname=mydb user=postgres')
    cur = conn.cursor()
    beginClause = "COPY ("
    selectClause = """SELECT '%s' FROM db """ % ','.join(self.flags)
    whenClause = """WHERE 'start' BETWEEN '%s' AND '%s'""" % self.info['begin'] self.info['end']
    destClause = """) TO '/dest/%s' WITH CSV HEADER""" % self.name

    fullQuery = beginClause + selectClause + …
Run Code Online (Sandbox Code Playgroud)

python postgresql psycopg2

4
推荐指数
1
解决办法
3015
查看次数

在结构中引用布尔值以进行赋值

type MyStruct struct {

    IsEnabled *bool
}
Run Code Online (Sandbox Code Playgroud)

如何更改*IsEnabled = true的值

这些都不起作用:

*(MyStruct.IsEnabled) = true
*MyStruct.IsEnabled = true
MyStruct.*IsEnabled = true
Run Code Online (Sandbox Code Playgroud)

go

2
推荐指数
1
解决办法
2214
查看次数

标签 统计

go ×2

blocking ×1

postgresql ×1

psycopg2 ×1

python ×1

resources ×1