Commits

Ronny Pfannschmidt  committed 31650d4

add basic io

  • Participants
  • Parent commits a7f6592

Comments (0)

Files changed (2)

     def __init__(self, flags=None, default=True):
         assert default
 
+    def io(self, fd, events):
+        return io(self, fd, events)
 
     def timer(self, after, repeat=0):
         return timer(self, after, repeat)
     def run(self):
         gtk.main()
 
+class io(object):
+    def __init__(self, loop, fd, events):
+        self.loop = loop
+        self.fd = fd
+        self.events = events
+        self.callback = None
+        self.args = None
+
+    def start(self, callback, *args):
+        self.callback = callback
+        self.args = args
+
+        must_read = self.events & 1
+        must_write = self.events & 2
+
+        # we ignore prio io
+
+        #XXX: this base may be wrong, check the semantics
+        flags = glib.IO_ERR|glib.IO_HUP
+        if must_read:
+            flags |= glib.IO_IN
+        if must_write:
+            flags |= glib.IO_OUT
+
+        self.source_id = glib.io_add_watch(
+            self.fd, flags, self._callback)
+
+    def _callback(self, source, condition):
+        print 'io callback', source, condition
+        try:
+            self.callback(*self.args)
+        except:
+            self.loop.handle_error(callback, *sys.exc_info())
+
+    def stop(self):
+        self.callback = None
+        self.args = None
+        #XXX: check this never raises
+        glib.source_remove(self.source_id)
 
 class timer(object):
     def __init__(self, loop, after, repeat=0):
         self.after = after
         self.repeat = repeat
         self.source_id = None
+        self.callback = None
+        self.args = None
 
     def first_callback(self):
+        print 'sleep callback'
         try:
             self.callback(*self.args)
         except:
             self.loop.handle_error(callback, *sys.exc_info())
         return False
 
-
     def start(self, callback, *args):
         self.callback = callback
-        self.args=args
-
+        self.args = args
 
         self.source_id = glib.timeout_add(
             self.after*1000,
             self.first_callback)
 
     def stop(self):
+        self.callback = None
+        self.args = None
         #XXX: check this never raises
         glib.source_remove(self.source_id)
 

File somescript.py

 from gevent.hub import Hub
 
 Hub.loop_class = GtkLoop
-
+Hub.resolver_class = 'gevent.socket.BlockingResolver'
 
 import gevent
-gevent.sleep(3)
+#gevent.sleep(1)
+
+import gevent.socket
+
+sock = gevent.socket.create_connection(('127.0.0.1', 8000))
+sock.sendall('GET / HTTP/1.1\r\nHost: google.com\r\n\r\n')
+print sock.recv(1000)
+