小编lim*_*boy的帖子

使用pip安装包的多个版本

在我的应用程序中,我想使用:

  • packageA,这需要 packageX==1.3
  • packageB,这需要 packageX==1.4
  • packageX==1.5

如何安装packageX带pip的多个版本来处理这种情况?

python pip

63
推荐指数
2
解决办法
2万
查看次数

什么是龙卷风ioloop和龙卷风的工作流程?

我想知道龙卷风的内部工作流程,看过这篇文章,它很棒,但是我无法弄清楚

在ioloop.py中,有这样的功能

def add_handler(self, fd, handler, events):
    """Registers the given handler to receive the given events for fd."""
    self._handlers[fd] = handler
    self._impl.register(fd, events | self.ERROR)
Run Code Online (Sandbox Code Playgroud)

这是什么意思?每个请求都会触发add_handler,或者只是在init时触发一次?

每个socket连接都会生成一个文件描述符,或者它只生成一次?

ioloop和iostream之间的关系是什么?

httpserver如何与ioloop和iostream一起使用?

是否有任何工作流程图,所以我可以清楚地看到它?

对不起这些问题,我只是感到困惑

任何链接,建议,提示有帮助.非常感谢 :)

python tornado

27
推荐指数
1
解决办法
1万
查看次数

非阻塞Redis pubsub可能吗?

我想使用redis'pubsub来传输一些消息,但不希望被阻止使用listen,如下面的代码:

import redis
rc = redis.Redis()

ps = rc.pubsub()
ps.subscribe(['foo', 'bar'])

rc.publish('foo', 'hello world')

for item in ps.listen():
    if item['type'] == 'message':
        print item['channel']
        print item['data']
Run Code Online (Sandbox Code Playgroud)

最后一for节将阻止.我只是想检查一个给定的通道是否有数据,我该如何做到这一点?有check类似的方法吗?

python redis redis-py

26
推荐指数
5
解决办法
3万
查看次数

navigationController.navigationItem vs navigationItem

只是好奇,为什么设置self.navigationItem = ...工作,但self.navigationController.navigationItem失败了?这同样适用于self.toolbarItemsVS self.navigationController.toobarItems.

什么时候用self.navigationController.navigationItem

也许你会说,他们指的是不同的东西.但为什么self.navigationController.navigationBarHidden = YES隐藏导航栏.这不是指self.navigationController.navigationItem我想要的酒吧吗?

uinavigationcontroller uinavigationitem ios

24
推荐指数
1
解决办法
9148
查看次数

什么时候使用KVO?

我已经阅读了很多关于KVO的文档,但我仍然对何时使用它感到困惑.

如果objA想监视某个属性objB,如下:

self.objB = [[ObjB alloc] init];
[self.objB addObserver:self
            forKeyPath:@"address"
               options:0
               context:nil];
Run Code Online (Sandbox Code Playgroud)

所以,如果objB属性发生变化,并且只能更改self,为什么不这样做:

self.objB.property = @"newValue";
[self doSomethingBasedOnNewValueOfObjBnewProperty];
Run Code Online (Sandbox Code Playgroud)

代替

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context

{
    if(keyPath == @"address") {
        [self doSomethingBasedOnNewValueOfObjBnewProperty];
    }
}
Run Code Online (Sandbox Code Playgroud)

当与单例一起使用时可能很有用,例如self.objB = [ObjB sharedInstance],其中属性可能被其他对象更改.这是唯一的用例吗?

objective-c key-value-observing

15
推荐指数
2
解决办法
2786
查看次数

如何实现redis的pubsub超时功能?

我想使用Redis的pubsub功能来实现彗星,但pubsub没有超时,所以如果我ps.listen(),它会阻止,即使客户端关闭浏览器.

greenlet在生成进程时具有超时功能.但我不知道如何将它们组合在一起.

烧瓶的假

@app.route('/')
def comet():
    rc = redis.Redis()
    ps = rc.pubsub()
    ps.subscribe('foo')
    for item in ps.listen():
        if item['type'] == 'message':
            return item['data']
    # ps.listen() will block, so how to make it timeout after 30 s?
Run Code Online (Sandbox Code Playgroud)

python comet redis flask

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

如何限制/抵消sqlalchemy orm relation的结果?

如果我有一个用户模型和文章模型,用户和文章是一对多的关系.所以我可以访问这样的文章

user = session.query(User).filter(id=1).one()
print user.articles
Run Code Online (Sandbox Code Playgroud)

但这将列出用户的所有文章,如果我想将文章限制为10怎么办?在rails中有一种all()方法可以有限制/偏移.在sqlalchemy还有一种all()方法,但不采取参数,如何实现这一目标?

编辑:

它似乎user.articles[10:20]是有效的,但sql在查询中没有使用10/20.所以实际上它会加载所有匹配的数据,并在python中过滤?

python orm sqlalchemy

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

UICollectionViewCell中的子视图控制器

假设我想要实现Pinterest的pin页面,就像这样:

Pinterest的

这是我的方法:

  1. 做一个UICollectionViewController,pin的页面是一个UICollectionViewCell
  2. cell由两个组成:pin info child vc && waterfall child vc

然后出现了问题:如何重用子视图控制器?

一些伪代码:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    Pin *pin = self.dataSource[indexPath.row];
    // i have to create a new childVC, based on different indexPath.
    UITableViewController *pinInfoViewController = [[pinInfoViewController alloc] initWithPin:pin];
    [cell updatePinViewWithPin:pin];
    [self addChildViewController:pinInfoViewController];

    // Add waterfall view controller
}
Run Code Online (Sandbox Code Playgroud)

每次调用此方法时,都会创建一个新的子视图控制器,它没问题,或者如何改进它?

objective-c ios childviewcontroller uicollectionview

6
推荐指数
1
解决办法
5213
查看次数

用元类实现单例

下面的代码是我如何使用元类实现单例,并且它运行良好

class Test_MetaClass(type):

    def __init__(cls, name, bases, dict):
        super(Test_MetaClass, cls).__init__(cls, bases, dict)
        cls._instance = None
        print 'Test_MetaClass __init__'

    def __call__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(Test_MetaClass, cls).__call__(*args, **kwargs)
        print 'Test_MetaClass __call__'
        return cls._instance


class A(object):
    __metaclass__ = Test_MetaClass
    def __init__(self):
        print 'A __init__ triggered'

a = A()
b = A()
Run Code Online (Sandbox Code Playgroud)

输出:

Test_MetaClass __init__
A __init__ triggered
Test_MetaClass __call__
Test_MetaClass __call__
Run Code Online (Sandbox Code Playgroud)

我的问题是为什么b = A()直接去Test_MetaClass.__call__和忽略__init__

python metaclass python-2.x

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

"当使用python的子进程和rsync时,"filedescriptor超出select()中的范围"

以下代码用于将上传的图片同步到另一个地方.它工作正常,但经过一段时间(大约10天)后,服务无法使用,显示错误:'filedescriptor超出了select()'范围,但重启服务解决了问题.

# sync.py

def sync_file(source_pic, hashval, retry_num=3):

    pic_path = os.path.join(gen_dir(hashval), os.path.split(source_pic)[1])
    filename = tempfile.mkstemp()[1]
    with open(filename, 'w') as f:
        f.write(pic_path)

    for sync_path in options.sync_paths:
        try_num = 0
        rsync_cmd = ['rsync','-au', '--files-from='+filename, options.pic_path, sync_path]

        while try_num < retry_num:
            proc = subprocess.Popen(rsync_cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            stdout_value, stderr_value = proc.communicate()

            if len(stderr_value) != 0:
                logger.error('sync failed: %s' % stderr_value)
                try_num = try_num + 1
                #raise UploadException('sync failed')
            else:
                break

    os.remove(filename)
Run Code Online (Sandbox Code Playgroud)

日志信息:

File "/path/to/sync.py", line 25, in sync_file
    stdout_value, stderr_value = proc.communicate()
File "/usr/lib/python2.6/subprocess.py", line 691, …
Run Code Online (Sandbox Code Playgroud)

python rsync subprocess

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