虽然异步IO(带有select/poll/epoll/kqueue等的非阻塞描述符)并不是Web上记录最多的东西,但有一些很好的例子.
但是,所有这些示例在确定了调用返回的句柄后,只有一个do_some_io(fd)"存根".它们并没有真正解释如何在这种方法中最好地接近实际的异步IO.
阻止IO非常整洁,直接读取代码.另一方面,非阻塞,异步IO是毛茸茸的,凌乱的.
有什么办法?什么是健壮和可读的?
void do_some_io(int fd) {
switch(state) {
case STEP1:
... async calls
if(io_would_block)
return;
state = STEP2;
case STEP2:
... more async calls
if(io_would_block)
return;
state = STEP3;
case STEP3:
...
}
}
Run Code Online (Sandbox Code Playgroud)
或者(ab)使用GCC的计算得到的:
#define concatentate(x,y) x##y
#define async_read_xx(var,bytes,line) \
concatentate(jmp,line): \
if(!do_async_read(bytes,&var)) { \
schedule(EPOLLIN); \
jmp_read = &&concatentate(jmp,line); \
return; \
}
// macros for making async code read like sync code
#define async_read(var,bytes) \
async_read_xx(var,bytes,__LINE__)
#define async_resume() \
if(jmp_read) { \
void* …Run Code Online (Sandbox Code Playgroud) 为什么Django网络服务器会阻塞,而不是像Tornado那样无阻塞?是否有理由以这种方式设计网络服务器?
蟒蛇,请帮忙.我希望这段代码可以询问某人是否可以获得成绩.如果他们说是,那就打印好了!如果他们说不,那就说哦!如果他们不说是或否,我希望它打印"请输入是或否"并继续问他们,直到他们最后说是或否.这是我到目前为止,当我运行它并且不输入是或否,它垃圾邮件"请输入是或否"数百万时间
theanswer= raw_input("Are you ok with that grade?")
while theanswer:
if theanswer == ("yes"):
print ("good!")
break
elif theanswer == ("no"):
print ("oh well")
break
else:
print "enter yes or no"
Run Code Online (Sandbox Code Playgroud)
我需要做什么才能使它工作,我一直在努力
python ×2
asynchronous ×1
blocking ×1
c ×1
c++ ×1
django ×1
if-statement ×1
io ×1
raw-input ×1
tornado ×1
webserver ×1
while-loop ×1