Esempio n. 1
0
def Worker(sock, addr):
    write_channel = stackless.channel()
    write_channel.preference = 1  # Prefer the sender.
    now_ts = time.time()
    timestamps = [now_ts, now_ts]  # read_ts, write_ts
    writer_tasklet = stackless.tasklet(Writer)(timestamps, write_channel, sock)
    sleeper_tasklet = stackless.tasklet(Sleeper)(timestamps, write_channel, 3)
    # TODO(pts): Flush earlier.
    write_channel.send('Hello, please type something.\r\n')
    try:
        while True:
            msg = sock.recv(256)
            if not msg:
                break
            timestamps[0] = max(timestamps[0], time.time())  # Register read.
            # TODO(pts): Flush earlier.
            write_channel.send('You typed %r.\r\n' % msg)
    finally:
        logging.info('connection closed from %r' % (addr, ))
        if writer_tasklet.alive:
            write_channel.send(None)  # Will kill writer_tasklet eventually.
        timestamps[0] = None  # Will kill sleeper_tasklet eventually.
        while writer_tasklet.alive or sleeper_tasklet.alive:
            stackless.schedule(None)
        sock.close()
Esempio n. 2
0
def Worker(sock, addr):
  write_channel = stackless.channel()
  write_channel.preference = 1  # Prefer the sender.
  now_ts = time.time()
  timestamps = [now_ts, now_ts]  # read_ts, write_ts
  writer_tasklet = stackless.tasklet(Writer)(timestamps, write_channel, sock)
  sleeper_tasklet = stackless.tasklet(Sleeper)(timestamps, write_channel, 3)
  # TODO(pts): Flush earlier.
  write_channel.send('Hello, please type something.\r\n')
  try:
    while True:
      msg = sock.recv(256)
      if not msg:
        break
      timestamps[0] = max(timestamps[0], time.time())  # Register read.
      # TODO(pts): Flush earlier.
      write_channel.send('You typed %r.\r\n' % msg)
  finally:
    logging.info('connection closed from %r' % (addr,))
    if writer_tasklet.alive:
      write_channel.send(None)  # Will kill writer_tasklet eventually.
    timestamps[0] = None  # Will kill sleeper_tasklet eventually.
    while writer_tasklet.alive or sleeper_tasklet.alive:
      stackless.schedule(None)
    sock.close()
def main():
    # Without patch.patch_socket() or patch.patch_pymysql() the
    # counter below would jump from 0 to 1000 in one big step. With this
    # patch, MySQL socket communication is done with Syncless, so the counter
    # increases in little steps.
    patch.patch_pymysql()
    # patch_socket() works instead of patch_pymysql(), but it effects more
    # Python modules.
    #patch.patch_socket()
    patch.patch_stderr()

    mysql_config = dict(MYSQL_CONFIG)
    if 'password' in mysql_config:
        mysql_config['passwd'] = mysql_config.pop('password')
    if 'database' in mysql_config:
        mysql_config['db'] = mysql_config.pop('database')
    if mysql_config.get('unix_socket'):
        mysql_config['host'] = '127.0.0.1'
    #mysql_config['charset'] = 'utf8'
    db_conn = mysql_dbapi.connect(**mysql_config)
    assert mysql_dbapi.paramstyle == 'format'
    cursor = db_conn.cursor()
    cursor.execute('SET NAMES %s COLLATE %s', ('utf8', 'utf8_general_ci'))
    cursor = db_conn.cursor()
    # In SQLite, this would be:
    # for row in cursor.execute('SELECT LENGTH(?), ('\xC3\xA1',)): print row
    cursor.execute('SELECT CHAR_LENGTH(%s)', ('\xC3\xA1', ))
    #for row in cursor:  # Fetch results.
    #  print >>sys.stderr, row
    assert list(cursor) == [(1, )]

    if len(sys.argv) > 1:
        num_iterations = int(sys.argv)
    else:
        num_iterations = 1000

    progress_channel = stackless.channel()
    progress_channel.preference = 1  # Prefer the sender.
    stackless.tasklet(Worker)(db_conn, num_iterations, progress_channel)
    done_count = 0
    receive_count = 0
    while True:
        sys.stderr.write('\r%s of %s ' % (done_count, num_iterations))
        if done_count >= num_iterations:
            break
        done_count = progress_channel.receive()
        receive_count += 1
    # receive_count might be smaller than done_count (e.g. 993 < 1000) here
    # (but sometims it's equal), because sometimes the main tasklet was slow
    # to receive, so Worker did multiple iterations per one
    # progress_channel.receive().
    sys.stderr.write('done, receive_count=%d\n' % receive_count)
    # Needed for exit because we might have done DNS lookups with coio (evdns).
    stackless.main.insert()
    sys.exit(0)
def main():
    # Without patch.patch_socket() or patch.patch_pymysql() the
    # counter below would jump from 0 to 1000 in one big step. With this
    # patch, MySQL socket communication is done with Syncless, so the counter
    # increases in little steps.
    patch.patch_pymysql()
    # patch_socket() works instead of patch_pymysql(), but it effects more
    # Python modules.
    # patch.patch_socket()
    patch.patch_stderr()

    mysql_config = dict(MYSQL_CONFIG)
    if "password" in mysql_config:
        mysql_config["passwd"] = mysql_config.pop("password")
    if "database" in mysql_config:
        mysql_config["db"] = mysql_config.pop("database")
    if mysql_config.get("unix_socket"):
        mysql_config["host"] = "127.0.0.1"
    # mysql_config['charset'] = 'utf8'
    db_conn = mysql_dbapi.connect(**mysql_config)
    assert mysql_dbapi.paramstyle == "format"
    cursor = db_conn.cursor()
    cursor.execute("SET NAMES %s COLLATE %s", ("utf8", "utf8_general_ci"))
    cursor = db_conn.cursor()
    # In SQLite, this would be:
    # for row in cursor.execute('SELECT LENGTH(?), ('\xC3\xA1',)): print row
    cursor.execute("SELECT CHAR_LENGTH(%s)", ("\xC3\xA1",))
    # for row in cursor:  # Fetch results.
    #  print >>sys.stderr, row
    assert list(cursor) == [(1,)]

    if len(sys.argv) > 1:
        num_iterations = int(sys.argv)
    else:
        num_iterations = 1000

    progress_channel = stackless.channel()
    progress_channel.preference = 1  # Prefer the sender.
    stackless.tasklet(Worker)(db_conn, num_iterations, progress_channel)
    done_count = 0
    receive_count = 0
    while True:
        sys.stderr.write("\r%s of %s " % (done_count, num_iterations))
        if done_count >= num_iterations:
            break
        done_count = progress_channel.receive()
        receive_count += 1
    # receive_count might be smaller than done_count (e.g. 993 < 1000) here
    # (but sometims it's equal), because sometimes the main tasklet was slow
    # to receive, so Worker did multiple iterations per one
    # progress_channel.receive().
    sys.stderr.write("done, receive_count=%d\n" % receive_count)
    # Needed for exit because we might have done DNS lookups with coio (evdns).
    stackless.main.insert()
    sys.exit(0)
Esempio n. 5
0
def main():
  # Without patch.patch_socket() or patch.patch_mysql_connector() the
  # counter below would jump from 0 to 1000 in one big step. With this
  # patch, MySQL socket communication is done with Syncless, so the counter
  # increases in little steps.
  patch.patch_mysql_connector()
  # patch_socket() works instead of patch_mysql_connector(), but it effects more
  # Python modules.
  #patch.patch_socket()
  patch.patch_stderr()

  db_conn = mysql_dbapi.connect(**MYSQL_CONFIG)
  assert mysql_dbapi.paramstyle == 'pyformat'
  assert db_conn.charset_name == 'utf8'
  assert db_conn.collation_name == 'utf8_general_ci'

  #query = 'SELECT CONNECTION_ID()'
  #query = 'SELECT LENGTH("\xC3\xA1")'  # :2
  #query = 'SELECT CHAR_LENGTH("\xC3\xA1")'  #: 1
  #query = 'SELECT UPPER("\xC3\xA1")'  #: '\xC3\x81'
  # Would raise e.g. mysql.connector.errors.ProgrammingError on SQL error.
  cursor = db_conn.cursor()
  # In SQLite, this would be:
  # for row in cursor.execute('SELECT LENGTH(?), ('\xC3\xA1',)): print row
  cursor.execute('SELECT CHAR_LENGTH(%s)', ('\xC3\xA1',))
  #for row in cursor:  # Fetch results.
  #  print >>sys.stderr, row
  assert list(cursor) == [(1,)]

  if len(sys.argv) > 1:
    num_iterations = int(sys.argv)
  else:
    num_iterations = 1000

  progress_channel = stackless.channel()
  progress_channel.preference = 1  # Prefer the sender.
  stackless.tasklet(Worker)(db_conn, num_iterations, progress_channel)
  done_count = 0
  receive_count = 0
  while True:
    sys.stderr.write('\r%s of %s ' % (done_count, num_iterations))
    if done_count >= num_iterations:
      break
    done_count = progress_channel.receive()
    receive_count += 1
  # receive_count might be smaller than done_count (e.g. 993 < 1000) here
  # (but sometims it's equal), because sometimes the main tasklet was slow
  # to receive, so Worker did multiple iterations per one
  # progress_channel.receive().
  sys.stderr.write('done, receive_count=%d\n' % receive_count)
  # Needed for exit because we might have done DNS lookups with coio (evdns).
  stackless.main.insert()
  sys.exit(0)
Esempio n. 6
0
def main():
    # Without patch.patch_socket() or patch.patch_mysql_connector() the
    # counter below would jump from 0 to 1000 in one big step. With this
    # patch, MySQL socket communication is done with Syncless, so the counter
    # increases in little steps.
    patch.patch_mysql_connector()
    # patch_socket() works instead of patch_mysql_connector(), but it effects more
    # Python modules.
    #patch.patch_socket()
    patch.patch_stderr()

    db_conn = mysql_dbapi.connect(**MYSQL_CONFIG)
    assert mysql_dbapi.paramstyle == 'pyformat'
    assert db_conn.charset_name == 'utf8'
    assert db_conn.collation_name == 'utf8_general_ci'

    #query = 'SELECT CONNECTION_ID()'
    #query = 'SELECT LENGTH("\xC3\xA1")'  # :2
    #query = 'SELECT CHAR_LENGTH("\xC3\xA1")'  #: 1
    #query = 'SELECT UPPER("\xC3\xA1")'  #: '\xC3\x81'
    # Would raise e.g. mysql.connector.errors.ProgrammingError on SQL error.
    cursor = db_conn.cursor()
    # In SQLite, this would be:
    # for row in cursor.execute('SELECT LENGTH(?), ('\xC3\xA1',)): print row
    cursor.execute('SELECT CHAR_LENGTH(%s)', ('\xC3\xA1', ))
    #for row in cursor:  # Fetch results.
    #  print >>sys.stderr, row
    assert list(cursor) == [(1, )]

    if len(sys.argv) > 1:
        num_iterations = int(sys.argv)
    else:
        num_iterations = 1000

    progress_channel = stackless.channel()
    progress_channel.preference = 1  # Prefer the sender.
    stackless.tasklet(Worker)(db_conn, num_iterations, progress_channel)
    done_count = 0
    receive_count = 0
    while True:
        sys.stderr.write('\r%s of %s ' % (done_count, num_iterations))
        if done_count >= num_iterations:
            break
        done_count = progress_channel.receive()
        receive_count += 1
    # receive_count might be smaller than done_count (e.g. 993 < 1000) here
    # (but sometims it's equal), because sometimes the main tasklet was slow
    # to receive, so Worker did multiple iterations per one
    # progress_channel.receive().
    sys.stderr.write('done, receive_count=%d\n' % receive_count)
    # Needed for exit because we might have done DNS lookups with coio (evdns).
    stackless.main.insert()
    sys.exit(0)
Esempio n. 7
0
def main():
    patch.patch_socket()
    patch.patch_ssl()
    result_channel = stackless.channel()
    result_channel.preference = 1  # Prefer the sender.
    stackless.tasklet(FetchWorker)('https://www.facebook.com/', result_channel)
    progress_reporter_tasklet = stackless.tasklet(ProgressReporter)(0.02)
    # This blocks the current tasklet, while FetchWorker and ProgressReporter are
    # running.
    data = result_channel.receive()
    progress_reporter_tasklet.kill()
    sys.stderr.write("\n")
    match = re.search(r'(?is)<title>(.*?)</title>', data)
    if match:
        data = match.group(1).strip()
    print 'Downloaded:', data
    # Needed for exit because we did DNS lookups with coio (evdns).
    # TODO(pts): Remove stackless.main.insert() once the segfault bug is fixed.
    stackless.main.insert()
    sys.exit(0)
Esempio n. 8
0
def main():
  patch.patch_socket()
  patch.patch_ssl()
  result_channel = stackless.channel()
  result_channel.preference = 1  # Prefer the sender.
  stackless.tasklet(FetchWorker)('https://www.facebook.com/', result_channel)
  progress_reporter_tasklet = stackless.tasklet(ProgressReporter)(0.02)
  # This blocks the current tasklet, while FetchWorker and ProgressReporter are
  # running.
  data = result_channel.receive()
  progress_reporter_tasklet.kill()
  sys.stderr.write("\n")
  match = re.search(r'(?is)<title>(.*?)</title>', data)
  if match:
    data = match.group(1).strip()
  print 'Downloaded:', data
  # Needed for exit because we did DNS lookups with coio (evdns).
  # TODO(pts): Remove stackless.main.insert() once the segfault bug is fixed.
  stackless.main.insert()
  sys.exit(0)
def main():
  # Without patch.geventmysql() run by importing fast_mysql, not only the
  # counter below would jump from 0 to 1000 in one big step, but maybe the
  # client wouldn't work at all, because vanilla gevenymysql expects gevent,
  # but we use Syncless here. With this patch, MySQL socket communication is
  # done with Syncless, so the counter increases in little steps.

  patch.patch_stderr()

  # Preprocess the connection information.
  mysql_config = dict(MYSQL_CONFIG)
  if mysql_config.get('unix_socket'):
    mysql_config['host'] = mysql_config.pop('unix_socket')
    mysql_config['port'] = None
    assert mysql_config['host'].startswith('/')
  if 'database' in mysql_config:
    mysql_config['db'] = mysql_config.pop('database')
  old_use_unicode = bool(mysql_config.pop('use_unicode', False))
  mysql_config['use_unicode'] = True  # Required for mysql_config['charset'].
  mysql_config.setdefault('charset', 'utf-8')
  db_conn = geventmysql.connect(**mysql_config)
  db_conn.client.set_use_unicode(old_use_unicode)
  assert geventmysql.paramstyle == 'format'

  # These are not supported by geventmysql.
  #assert db_conn.charset_name == 'utf8'
  #assert db_conn.collation_name == 'utf8_general_ci'

  #query = 'SELECT CONNECTION_ID()'
  #query = 'SELECT LENGTH("\xC3\xA1")'  # :2
  #query = 'SELECT CHAR_LENGTH("\xC3\xA1")'  #: 1
  #query = 'SELECT UPPER("\xC3\xA1")'  #: '\xC3\x81'
  # Would raise e.g. mysql.connector.errors.ProgrammingError on SQL error.
  cursor = db_conn.cursor()
  # In SQLite, this would be:
  # for row in cursor.execute('SELECT LENGTH(?), ('\xC3\xA1',)): print row
  cursor.execute('SELECT CHAR_LENGTH(%s)', ('\xC3\xA1',))

  # Since geventmysql cursors are not iterable, we have to use
  # cursor.fetchall() instead of list(cursor) here.
  assert cursor.fetchall() == [(1,)]
  cursor.close()  # geventmysql requires this.

  if len(sys.argv) > 1:
    num_iterations = int(sys.argv)
  else:
    num_iterations = 1000

  progress_channel = stackless.channel()
  progress_channel.preference = 1  # Prefer the sender.
  stackless.tasklet(Worker)(db_conn, num_iterations, progress_channel)
  done_count = 0
  receive_count = 0
  while True:
    sys.stderr.write('\r%s of %s ' % (done_count, num_iterations))
    if done_count >= num_iterations:
      break
    done_count = progress_channel.receive()
    receive_count += 1
  # receive_count might be smaller than done_count (e.g. 993 < 1000) here
  # (but sometims it's equal), because sometimes the main tasklet was slow
  # to receive, so Worker did multiple iterations per one
  # progress_channel.receive().
  sys.stderr.write('done, receive_count=%d\n' % receive_count)
  # Needed for exit because we might have done DNS lookups with coio (evdns).
  stackless.main.insert()
  sys.exit(0)
Esempio n. 10
0
 def __init__(self, items=(), preference=1):
     self.deque = deque(items)
     self.channel = stackless.channel()
     self.channel.preference = preference  # preference=1: prefer the sender.
def main():
    # Without patch.geventmysql() run by importing fast_mysql, not only the
    # counter below would jump from 0 to 1000 in one big step, but maybe the
    # client wouldn't work at all, because vanilla gevenymysql expects gevent,
    # but we use Syncless here. With this patch, MySQL socket communication is
    # done with Syncless, so the counter increases in little steps.

    patch.patch_stderr()

    # Preprocess the connection information.
    mysql_config = dict(MYSQL_CONFIG)
    if mysql_config.get('unix_socket'):
        mysql_config['host'] = mysql_config.pop('unix_socket')
        mysql_config['port'] = None
        assert mysql_config['host'].startswith('/')
    if 'database' in mysql_config:
        mysql_config['db'] = mysql_config.pop('database')
    old_use_unicode = bool(mysql_config.pop('use_unicode', False))
    mysql_config['use_unicode'] = True  # Required for mysql_config['charset'].
    mysql_config.setdefault('charset', 'utf-8')
    db_conn = geventmysql.connect(**mysql_config)
    db_conn.client.set_use_unicode(old_use_unicode)
    assert geventmysql.paramstyle == 'format'

    # These are not supported by geventmysql.
    #assert db_conn.charset_name == 'utf8'
    #assert db_conn.collation_name == 'utf8_general_ci'

    #query = 'SELECT CONNECTION_ID()'
    #query = 'SELECT LENGTH("\xC3\xA1")'  # :2
    #query = 'SELECT CHAR_LENGTH("\xC3\xA1")'  #: 1
    #query = 'SELECT UPPER("\xC3\xA1")'  #: '\xC3\x81'
    # Would raise e.g. mysql.connector.errors.ProgrammingError on SQL error.
    cursor = db_conn.cursor()
    # In SQLite, this would be:
    # for row in cursor.execute('SELECT LENGTH(?), ('\xC3\xA1',)): print row
    cursor.execute('SELECT CHAR_LENGTH(%s)', ('\xC3\xA1', ))

    # Since geventmysql cursors are not iterable, we have to use
    # cursor.fetchall() instead of list(cursor) here.
    assert cursor.fetchall() == [(1, )]
    cursor.close()  # geventmysql requires this.

    if len(sys.argv) > 1:
        num_iterations = int(sys.argv)
    else:
        num_iterations = 1000

    progress_channel = stackless.channel()
    progress_channel.preference = 1  # Prefer the sender.
    stackless.tasklet(Worker)(db_conn, num_iterations, progress_channel)
    done_count = 0
    receive_count = 0
    while True:
        sys.stderr.write('\r%s of %s ' % (done_count, num_iterations))
        if done_count >= num_iterations:
            break
        done_count = progress_channel.receive()
        receive_count += 1
    # receive_count might be smaller than done_count (e.g. 993 < 1000) here
    # (but sometims it's equal), because sometimes the main tasklet was slow
    # to receive, so Worker did multiple iterations per one
    # progress_channel.receive().
    sys.stderr.write('done, receive_count=%d\n' % receive_count)
    # Needed for exit because we might have done DNS lookups with coio (evdns).
    stackless.main.insert()
    sys.exit(0)
Esempio n. 12
0
 def __init__(self, items=(), preference=1):
     self.deque = deque(items)
     self.channel = stackless.channel()
     self.channel.preference = preference  # preference=1: prefer the sender.