File: mysql.py

package info (click to toggle)
python-greenio 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 144 kB
  • ctags: 200
  • sloc: python: 988; makefile: 32
file content (76 lines) | stat: -rw-r--r-- 2,378 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""PyMySQL example"""
import asyncio
import socket
from pymysql import connections
from greenio import socket as greensocket


class GreenConnection(connections.Connection):

    def _connect(self):
        try:
            if self.unix_socket:
                raise NotImplementedError()
            else:
                sock = greensocket.socket(socket.AF_INET, socket.SOCK_STREAM)
                sock.connect((self.host, self.port))
                self.host_info = "socket %s:%d" % (self.host, self.port)
            if self.no_delay:
                sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            self.socket = sock
            self.rfile = self.socket.makefile("rb")
            self.wfile = self.socket.makefile("wb")
            self._get_server_information()
            self._request_authentication()
            self._send_autocommit_mode()
        except socket.error as e:
            raise Exception(
                2003, "Can't connect to MySQL server on %r (%s)" % (
                    self.host, e.args[0]))


if __name__ == '__main__':
    import greenio
    import time

    @asyncio.coroutine
    def sleeper():
        # show that we're not blocked
        while True:
            yield from asyncio.sleep(0.2)
            print('.')

    @greenio.task
    def db():
        conn = GreenConnection(host='localhost')

        try:
            with conn as cur:
                print('>> sleeping')
                st = time.monotonic()
                cur.execute('SELECT SLEEP(2)')
                en = time.monotonic() - st
                assert en >= 2
                print('<< sleeping {:.3f}s'.format(en))

                cur.execute('SELECT 42')
                print('"SELECT 42" -> {!r}'.format(cur.fetchone()))

                print('>> sleeping')
                st = time.monotonic()
                cur.execute('SELECT SLEEP(1)')
                en = time.monotonic() - st
                assert en >= 1
                print('<< sleeping {:.3f}s'.format(en))
        finally:
            conn.close()

    @asyncio.coroutine
    def run():
        yield from asyncio.wait([db(), sleeper()],
                              return_when=asyncio.FIRST_COMPLETED)

    asyncio.set_event_loop_policy(greenio.GreenEventLoopPolicy())
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())
    loop.close()