Commits

Antoine Pietri committed b18f3ed

move psycopg wait() to use tornado's event loop

Comments (0)

Files changed (2)

python-lib/prologin/master-node/task.py

 class CompilationTask:
     def __init__(self, config, user, champ_id):
         self.contest = config['master']['contest']
-        self.secret = config['master']['shared_secret']
         self.user = user
         self.champ_id = champ_id
 
 
     def execute(self, master, worker):
         worker.rpc.compile_champion(
-            self.secret, self.contest, self.user, self.champ_id
+            self.contest, self.user, self.champ_id
         )
 
     def __repr__(self):
 class PlayerTask:
     def __init__(self, config, mid, hostname, req_port, sub_port, cid, mpid, user, opts):
         self.contest = config['master']['contest']
-        self.secret = config['master']['shared_secret']
         self.mid = mid
         self.hostname = hostname
         self.req_port = req_port
 
     def execute(self, master, worker):
         worker.rpc.run_client(
-            self.secret, self.contest, self.mid, self.hostname, self.req_port,
+            self.contest, self.mid, self.hostname, self.req_port,
             self.sub_port, self.user, self.cid, self.mpid, self.opts
         )
 
     def __init__(self, config, mid, players, opts):
         self.config = config
         self.contest = config['master']['contest']
-        self.secret = config['master']['shared_secret']
         self.mid = mid
         self.players = players
         self.opts = opts
     def execute(self, master, worker):
         master.matches[self.mid] = self
         worker.rpc.run_server(
-            self.secret, self.contest, self.mid, self.opts
+            self.contest, self.mid, self.opts
         )
         req_port = self.server_port.get()
         sub_port = self.server_port.get()

python-lib/prologin/master-node/utils.py

 # You should have received a copy of the GNU General Public License
 # along with Stechec.  If not, see <http://www.gnu.org/licenses/>.
 
-import gevent.socket
 import psycopg2
 import psycopg2.extensions
+import tornado.ioloop
+import tornado.gen
 
 """
 Implements a wait function for Psycopg in order to wait for events while being
-nice with gevent (not blocking, etc.).
+nice with tornado (not blocking, etc.).
 
 See http://initd.org/psycopg/docs/advanced.html#asynchronous-support
+and https://gist.github.com/FSX/861193
 """
 
-def init_psycopg_gevent():
+ioloop = tornado.ioloop.IOLoop.instance()
+
+def init_psycopg_tornado():
+
+    @tornado.gen
     def wait(conn, timeout=None):
         while True:
             state = conn.poll()
             if state == psycopg2.extensions.POLL_OK:
                 break
             elif state == psycopg2.extensions.POLL_READ:
-                gevent.socket.wait_read(conn.fileno(), timeout=timeout)
+                ioloop.add_handler(conn.fileno(),
+                                   (yield gen.Callback('read_psycopg2')),
+                                   tornado.ioloop.IOLoop.READ)
+                yield tornado.gen.Wait('read_psycopg2')
             elif state == psycopg2.extensions.POLL_WRITE:
-                gevent.socket.wait_write(conn.fileno(), timeout=timeout)
+                ioloop.add_handler(conn.fileno(),
+                                   (yield gen.Callback('write_psycopg2')),
+                                   tornado.ioloop.IOLoop.WRITE)
+                yield tornado.gen.Wait('write_psycopg2')
             else:
                 raise psycopg2.OperationalError("invalid poll state")