請問使用 eurasia sleep 的功能如何一運行服務時自動運行?

8 views
Skip to first unread message

bawbaw hu

unread,
Dec 27, 2010, 2:42:33 AM12/27/10
to eurasi...@googlegroups.com
舉個例子
#!/usr/bin/python2.7/bin/python
# encoding: utf-8
from eurasia.web import httpserver, mainloop
from eurasia.core import sleep
from time import time
def handler(httpfile):
    httpfile['Content-Type'] = 'text/html'
    httpfile.write('<html>hello world!</html>')
    httpfile.close()

def loop():
    while True:
        print('test: %d'%time())
        sleep(1.)


httpd = httpserver(('', 6666), handler)
httpd.start()
mainloop()

如果我想一運行此程序,就自動運行 loop()

請問要如何弄呢?

我果我寫成
#!/usr/bin/python2.7/bin/python
# encoding: utf-8
from eurasia.web import httpserver, mainloop
from eurasia.core import sleep
from time import time
def handler(httpfile):
    httpfile['Content-Type'] = 'text/html'
    httpfile.write('<html>hello world!</html>')
    httpfile.close()

def loop():
    while True:
        print('test: %d'%time())
        sleep(1.)

loop()
httpd = httpserver(('', 6666), handler)
httpd.start()
mainloop()

會出現底下的錯誤
 ./main.py
test: 1293435344
Traceback (most recent call last):
  File "./main.py", line 16, in <module>
    loop()
  File "./main.py", line 14, in loop
    sleep(1.)
  File "/usr/bin/python2.7/lib/python2.7/site-packages/eurasia/core.py", line 22, in sleep
    gcurr.parent.switch()
AttributeError: 'NoneType' object has no attribute 'switch'

請問要怎麼弄比較好呢?

Wilhelm Shen

unread,
Dec 27, 2010, 4:12:45 AM12/27/10
to eurasi...@googlegroups.com
写成这样就可以了(要用到 greenlet,默认打包在 eurasia 中):

from eurasia.core import sleep
from eurasia.greenlet import greenlet


from eurasia.web import httpserver, mainloop

from time import time
def handler(httpfile):
httpfile['Content-Type'] = 'text/html'
httpfile.write('<html>hello world!</html>')
httpfile.close()

def loop():
while True:
print('test: %d'%time())
sleep(1.)

greenlet(loop).switch() # 使用 greenlet 启动一个协程

httpd = httpserver(('', 6666), handler)
httpd.start()
mainloop()

这部分内容我会增补到文档中。

bawbaw hu

unread,
Dec 27, 2010, 4:43:57 AM12/27/10
to eurasi...@googlegroups.com
謝謝沈大提點,好用的。
另外之前提的,wsgi 和 一般 httpserver 結合的目前進行的如何了呀

Wilhelm Shen <wile...@gmail.com> 於 2010年12月27日下午5:12 寫道:
写成这样就可以了(要用到 greenlet,默认打包在 eurasia 中):

from eurasia.core import sleep
from eurasia.greenlet import greenlet
from eurasia.web import httpserver, mainloop
from time import time
def handler(httpfile):
   httpfile['Content-Type'] = 'text/html'
   httpfile.write('<html>hello world!</html>')
   httpfile.close()

def loop():
   while True:
       print('test: %d'%time())
       sleep(1.)

greenlet(loop).switch() # 使用 greenlet 启动一个协程

httpd = httpserver(('', 6666), handler)
httpd.start()
mainloop()

这部分内容我会增补到文档中。

--
您收到此邮件是因为您订阅了 Google 网上论坛的"eurasia-users"论坛。
要向此网上论坛发帖,请发送电子邮件至 eurasi...@googlegroups.com
要取消订阅此网上论坛,请发送电子邮件至 eurasia-user...@googlegroups.com
若有更多问题,请通过 http://groups.google.com/group/eurasia-users?hl=zh-CN 访问此网上论坛。


老光

unread,
Dec 27, 2010, 10:48:52 PM12/27/10
to eurasi...@googlegroups.com
沈大咯, 与windows的兼容,有没有列入计划?
 
我希望在windows上安装stackless后,可以完美兼容windows.因为目前有一台外网机器,安装的XP(我用ISO文件刻盘后安装ubuntu,居然安不成功!!去安装老的RH7.2又嫌麻烦,很久没沾那东东了).上面安装stackless python 2.5.2,跑eurasia 3.0b1跑得很欢.
 
如短期没有计划,我就去折腾一下,安个linux.

沈崴

unread,
Dec 27, 2010, 11:46:09 PM12/27/10
to eurasia-users
On 12月27日, 上午9时43分, bawbaw hu <bawbaw...@gmail.com> wrote:
> 謝謝沈大提點,好用的。
> 另外之前提的,wsgi 和 一般 httpserver 結合的目前進行的如何了呀

working ... :)

沈崴

unread,
Dec 27, 2010, 11:47:33 PM12/27/10
to eurasia-users
On 12月28日, 上午3时48分, 老光 <yaoguangm...@cq.chinatelecom.com.cn> wrote:
> 沈大咯, 与windows的兼容,有没有列入计划?
>
> 我希望在windows上安装stackless后,可以完美兼容windows.因为目前有一台外网机器,
> 安装的XP(我用ISO文件刻盘后安装ubuntu,居然安不成功!!去安装老的RH7.2又嫌麻烦,
> 很久没沾那东东了).上面安装stackless python 2.5.2,跑eurasia 3.0b1跑得很欢.
>
> 如短期没有计划,我就去折腾一下,安个linux.

短期计划是有的,我现在正在研究,只是抱歉的是很难在短期内完 :(

Reply all
Reply to author
Forward
0 new messages