Contents
eurasia异步访问数据库
问题
老光 [email protected] 通过“googlegroups.com” 发件人当地时间 发送时间 15:54 (GMT+08:00)。发送地当前时间:下午4:46。 ✆ 发送至 [email protected] 主题 [eurasia-users] 老大,eurasia的协程,能解决异步访问数据库么?
比如访问sqlite3, mysql, pgsql?
- 以我现在的论坛为例,是sqlite3数据库,没有考虑异步的问题,因为发贴,查找等操作大都在1秒以内,在在线人数几十人的时候,不会出现什么问题。
 - 但接下来想加入统计方面的应用,需要连内网上的pgsql数据库服务器,执行复杂的sql语句。目前的情况,是不是若通过pgycopg2执行sql语句的话,就会阻塞其他协程?那有没有什么办法解决在一个用户需要统计几十秒的情况下,其他用户可以同时统计,或访问系统的其他部分?
 
支持 PostgreeSQ
沈崴 [email protected] 发件人当地时间 发送时间 15:02 (GMT-07:00)。发送地当前时间:上午1:47。 ✆ 发送至 eurasia-users <[email protected]>
在程序前面加上如下代码即可支持 PostgreeSQL:
   1 from eurasia import core
   2 from psycopg2 import extensions
   3 
   4 def wait_callback(conn, timeout=-1):
   5    file = core.file(conn.fileno())
   6    while 1:
   7        state = conn.poll()
   8        if state == extensions.POLL_OK:
   9            break
  10        elif state == extensions.POLL_READ:
  11            file.r_wait(timeout)
  12        elif state == extensions.POLL_WRITE:
  13            file.w_wait(timeout)
  14        else:
  15            raise psycopg2.OperationalError(
  16                'Bad result from poll: %r' % state)
  17 
  18 extensions.set_wait_callback(wait_callback)
反馈
创建 by -- ZoomQuiet [2011-09-27 08:48:41]