[Stackless] Help using stackless with threads

inhahe inhahe at gmail.com
Tue Jul 15 10:41:39 CEST 2008


Can someone help me with using stackless with python threads.  I'm
trying to make a thread pool for database access using stackless
channels to activate the threads because I can't use signals because
I'm on Windows.  So I run 4 threads (using the thread module) and each
thread runs 1 stackless tasklet in an infinite loop that waits to
receive from a unique channel.  But it seems all four of them fall
through the receive() call each iteration - whether I send something
or not - and I'm guessing it's because each one thinks that it's the
last tasklet running because it's the only tasklet in its thread.  So
how do I fix this?  Here's the code I have..

# dbserver.py

import MySQLdb, thread, stackless
from collections import deque

dbconns = 4

queue = deque()
sleeping = deque()

def request(sql, params, channel):
  queue.append((sql, params, channel))
  if sleeping: sleeping.pop().send(None)

def requestloop(connection, sleepchan):
  global sleeping, queue
  db = MySQLdb.connect(db="mercury", user="root", passwd="secret")
  cursor = db.cursor(MySQLdb.cursors.DictCursor)
  while 1:
    if not queue:
      sleeping.append(sleepchan)
      sleepchan.receive()                              # because
signals don't work on windows
                                                       # apparently
each tasklet thinks it's the only tasklet so receive falls through
      request, params, channel = queue.popleft()
      cursor.execute(request, params)
      channel.send(cursor.fetchall())

def start():
  threads = []
  for x in xrange(dbconns):
    threads.append(thread.start_new_thread(requestloop,(MySQLdb.connect(db="mercury",
user="root", passwd="secret").cursor(MySQLdb.cursors.DictCursor),stackless.channel())))




More information about the Stackless mailing list