小编lli*_*lib的帖子

popen中的超时工作,但是超时内的popen不行吗?

在代码中解释最简单:

require 'timeout'

puts "this block will properly kill the sleep after a second"

IO.popen("sleep 60") do |io|
  begin
    Timeout.timeout(1) do
      while (line=io.gets) do
        output += line
      end
    end
  rescue Timeout::Error => ex
    Process.kill 9, io.pid
    puts "timed out: this block worked correctly"
  end
end

puts "but this one blocks for >1 minute"

begin
  pid = 0
  Timeout.timeout(1) do
    IO.popen("sleep 60") do |io|
      pid = io.pid
      while (line=io.gets) do
        output += line
      end
    end
  end
rescue Timeout::Error => ex
  puts …
Run Code Online (Sandbox Code Playgroud)

ruby io multithreading subprocess timeout

12
推荐指数
1
解决办法
2317
查看次数

查找with:Block中定义的函数

以下是Richard Jones博客的一些代码:

with gui.vertical:
    text = gui.label('hello!')
    items = gui.selection(['one', 'two', 'three'])
    with gui.button('click me!'):
        def on_click():
            text.value = items.value
            text.foreground = red
Run Code Online (Sandbox Code Playgroud)

我的问题是:他是怎么做到的?上下文管理器如何访问with块内的范围?这是一个试图解决这个问题的基本模板:

from __future__ import with_statement

class button(object):
  def __enter__(self):
    #do some setup
    pass

  def __exit__(self, exc_type, exc_value, traceback):
    #XXX: how can we find the testing() function?
    pass

with button():
  def testing():
    pass
Run Code Online (Sandbox Code Playgroud)

python scope with-statement contextmanager

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

在golang中使用json marshaling测试深度相等性

鉴于这两个测试用例:

func TestEqualWhat(t *testing.T) {
    testMarshalUnmarshal(t, map[string]interface{}{"a":"b"})
    testMarshalUnmarshal(t, map[string]interface{}{"a":5})
}
Run Code Online (Sandbox Code Playgroud)

testMarshalUnmarshal帮助程序只是对json进行编组并返回:

func testMarshalUnmarshal(t *testing.T, in map[string]interface{}) {
    //marshal the object to a string
    jsb, err := json.Marshal(in);
    if err != nil {
        log.Printf("Unable to marshal msg")
        t.FailNow()
    }

    //unmarshal to a map
    res := make(map[string]interface{})
    if err := json.Unmarshal(jsb, &res); err != nil { t.FailNow() }

    if !reflect.DeepEqual(in, res) {
        log.Printf("\nExpected %#v\nbut got  %#v", in, res)
        t.FailNow()
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么第一个测试用例通过而第二个测试用例失败?测试的输出是这样的:

Expected map[string]interface {}{"a":5}
but got  map[string]interface {}{"a":5}
--- FAIL: TestEqualWhat …
Run Code Online (Sandbox Code Playgroud)

json go

9
推荐指数
1
解决办法
4343
查看次数

是否可以进入导轨2.3的导轨导轨?

过去,版本3.0的rails指南上的ruby位于http://edgeguides.rubyonrails.org/,版本2.3的指南位于http://guides.rubyonrails.org.现在版本3已经发布,其指南已移至主URL.

有没有办法访问rails 2.3的指南?

documentation ruby-on-rails

8
推荐指数
1
解决办法
466
查看次数

如何使用vgo和给定的go.mod下载所有依赖项?

我正在使用vgo和一个Dockerfile开展一个go项目,我希望docker能够缓存项目的依赖项,这样它就有两个属性:

  1. 如果go.mod文件更改,将重新下载依赖项
  2. 如果我更改了包中的文件但没有更改go.mod,则不会重新下载依赖项.

现在,我这样做:

...
RUN go get -u golang.org/x/vgo
COPY . /go/src/whatever
RUN vgo install
...
Run Code Online (Sandbox Code Playgroud)

但是,如果您更改了go文件,则必须从该COPY层重建dockerfile .

简而言之,我想做的是:

...
RUN go get -u golang.org/x/vgo
COPY go.mod /go/src/whatever
RUN vgo install_dependencies
COPY . /go/src/whatever
RUN vgo install
...
Run Code Online (Sandbox Code Playgroud)

这样,如果我更改go.mod,将下载并重建所有依赖项,但除此之外,我们可以继续构建二进制文件.

我可以看到几种方法来获得这样的行为,但所有这些方法都有缺点:

  1. 我可以复制$GOPATH/src/mod到docker容器中,但是这将有很多我不需要的文件
  2. 我可以vgo mod -vendor在构建docker容器之前复制供应商目录,但这依赖于开发人员记住vgo mod -vendor每次go.mod更改时都要运行,否则应用程序将无法构建,他们必须vgo mod -vendor在重试docker构建之前运行.

你能想到一种方式让我获得像我想象中的行为vgo install_dependencies吗?我错过了一个vgo技巧吗?

go docker vgo

8
推荐指数
1
解决办法
3782
查看次数

Golang net.Listen绑定到已使用的端口

端口8888已通过在docker容器中运行的进程绑定到我的(OS X 10.13.5)系统上:

$ netstat -an | grep 8888
tcp6       0      0  ::1.8888               *.*                    LISTEN
tcp4       0      0  *.8888                 *.*                    LISTEN
Run Code Online (Sandbox Code Playgroud)

尝试绑定到该端口的python程序(使用我可以管理的与golang尽可能接近的socket选项),以我期望的方式失败:

$ netstat -an | grep 8888
tcp6       0      0  ::1.8888               *.*                    LISTEN
tcp4       0      0  *.8888                 *.*                    LISTEN
Run Code Online (Sandbox Code Playgroud)

失败:

$ python test.py
Traceback (most recent call last):
  File "test.py", line 15, in <module>
    main()
  File "test.py", line 11, in main
    sock.bind(("0.0.0.0", 8888))
OSError: [Errno 48] Address already in use
Run Code Online (Sandbox Code Playgroud)

但是go程序通过创建连接net.Listen不会失败,正如我期望的那样:

package main

import (
    "fmt"
    "net" …
Run Code Online (Sandbox Code Playgroud)

python sockets macos networking go

5
推荐指数
1
解决办法
1663
查看次数

PyPy比Python快17倍.Python可以加速吗?

解决了最近出现的代码问题,我发现我的默认Python比PyPy慢约40倍.通过在函数中运行限制调用和限制全局查找,我能够通过此代码将其降低到大约17倍len.

现在,e.py在python 3.6.3上运行5.162秒,在我的机器上在PyPy上运行.297秒.

我的问题是:这是JIT的不可缩短的加速,还是有某种方法可以加速CPython的回答?(没有极端的意思:我可以去Cython/Numba或其他什么?)我如何说服自己,我无能为力?


请参阅gist以获取数字输入文件列表.

问题陈述所述,它们代表跳跃偏移.position += offsets[current],并将当前偏移量增加1.当跳转将您带到列表外时,您就完成了.

这是给出的示例(需要5秒的完整输入更长,并且具有更大的数字):

(0) 3  0  1  -3  - before we have taken any steps.
(1) 3  0  1  -3  - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction is then incremented to 1.
 2 (3) 0  1  -3  - step forward because of the instruction we just modified. The first instruction is incremented again, …
Run Code Online (Sandbox Code Playgroud)

python performance benchmarking pypy

3
推荐指数
2
解决办法
2634
查看次数

Postgres 似乎忽略了我的默认排序规则

我想将 postgres 设置为en-US-x-icu默认按排序规则排序,因此我将LC_CTYPELC_COLLATE环境变量设置为该值。Postgres 似乎看到了它们,并说它正在使用该排序规则:

:mctapi=# select datname, datcollate, datctype from pg_database where datname='mctapi';
 datname | datcollate  |  datctype
---------+-------------+-------------
 mctapi  | en-US-x-icu | en-US-x-icu

:mctapi=# show lc_collate;
 lc_collate
-------------
 en-US-x-icu
(1 row)

:mctapi=# show lc_ctype;
  lc_ctype
-------------
 en-US-x-icu
(1 row)
Run Code Online (Sandbox Code Playgroud)

但是,除非我明确设置en-US-x-icu为排序规则,否则我会得到C排序规则:

:mctapi=# select distinct organization_name from plans order by organization_name limit 5;
            organization_name
------------------------------------------
 AMH Health
 ATRIO Health Plans
 Aetna Medicare
 AgeRight Advantage
 AgeRight Advantage Health Plan (HMO SNP)
(5 …
Run Code Online (Sandbox Code Playgroud)

sql database postgresql

3
推荐指数
1
解决办法
1541
查看次数