我没有尝试使用txredis(redis的非阻塞扭曲api)作为持久消息队列,我正在尝试使用我正在处理的scrapy项目进行设置.我发现虽然客户端没有阻塞,但它变得比它本来要慢得多,因为应该在反应器循环中的一个事件被分成数千个步骤.
所以相反,我尝试使用redis-py(常规阻塞扭曲api)并将调用包装在延迟线程中.它工作得很好,但是当我打电话给redis时我想要执行内部延迟,因为我想设置连接池以试图进一步加快速度.
下面是我对一些延迟线程的扭曲文档中的一些示例代码的解释,以说明我的用例:
#!/usr/bin/env python
from twisted.internet import reactor,threads
from twisted.internet.task import LoopingCall
import time
def main_loop():
print 'doing stuff in main loop.. do not block me!'
def aBlockingRedisCall():
print 'doing lookup... this may take a while'
time.sleep(10)
return 'results from redis'
def result(res):
print res
def main():
lc = LoopingCall(main_loop)
lc.start(2)
d = threads.deferToThread(aBlockingRedisCall)
d.addCallback(result)
reactor.run()
if __name__=='__main__':
main()
Run Code Online (Sandbox Code Playgroud)
这是我对连接池的更改,使得延迟线程中的代码阻塞:
#!/usr/bin/env python
from twisted.internet import reactor,defer
from twisted.internet.task import LoopingCall
import time
def main_loop():
print 'doing stuff in main …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试在调用函数之前是否定义了声明为void的指针的结构的成员.但是,似乎每当我向裸骨程序添加额外的东西时(例如最后的std :: cout),'testB'总是等于true.我无法改变'struct a'的声明方式,因为它是外部库的一部分.
那么,我如何测试声明为void指针的结构的成员实际上是用正确的类型定义的?
struct b { };
struct a {
void *b;
};
Run Code Online (Sandbox Code Playgroud)
struct a myA;
struct b myB;
myA.b = &myB;
foo(myA);
Run Code Online (Sandbox Code Playgroud)
#include <iostream>
int main() {
struct a aa;
struct b bb;
// the following line is commented out so I'm expecting 'testB' to evaluate to false
// aa.b = &bb;
struct b *testB = static_cast<struct b*>(aa.b);
if(testB) {
std::cout << "is defined" << std::endl;
} else {
std::cout << "is not defined" …Run Code Online (Sandbox Code Playgroud)