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)
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)
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)
sys.stdout.flush() answer = sys.stdin.readline() assert answer answer = answer.strip() try: age = int(answer) except ValueError: print 'Please enter an integer.' continue if age < 3: print 'That would be too young. Please enter a valid age.' continue age_answer_channel.send(age) return if __name__ == '__main__': patch.patch_stdin_and_stdout() # sets sys.stdin = sys.stdout = ... patch.patch_stderr() # For fair exeption reporting. age_answer_channel = coio.stackless.channel() age_answer_channel.preference = 1 # Prefer the sender. timeout = 3 asker_tasklet = coio.stackless.tasklet(Asker)(timeout, age_answer_channel) age = coio.receive_with_timeout(timeout, age_answer_channel) if age is None: # Timed out. if asker_tasklet.alive: asker_tasklet.kill() print 'You were too slow entering your age.' else: print 'Got age: %r.' % age
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)
ss.bind(addr) ss.listen(128) print >>sys.stderr, 'info: listening for telnet chat on %r' % ( ss.getsockname(),) print >>sys.stderr, 'info: to connect, run: telnet %s %s' % ( ss.getsockname()[:2]) while True: cs, csaddr = ss.accept() print >>sys.stderr, 'info: connection from %r' % (csaddr,) coio.stackless.tasklet(ChatWorker)(cs.makefile(), csaddr) cs = csaddr = None # Free memory early. if __name__ == '__main__': patch.patch_stdin_and_stdout() # sets sys.stdin = sys.stdout = ... patch.patch_stderr() # For fair exeption reporting. assert sys.stdin is sys.stdout server_host = ('127.0.0.1') port = 1337 if len(sys.argv) > 2: server_host = sys.argv[1] server_port = int(sys.argv[2]) elif len(sys.argv) > 1: port = int(sys.argv[1]) broadcast_channel = coio.stackless.channel() broadcast_channel.preference = 1 # Prefer the sender. coio.stackless.tasklet(BroadcastWorker)() coio.stackless.tasklet(ChatListener)((server_host, port)) # sys.stdin here can be used for writing, as set up by patch_stdin_and_stdout coio.stackless.tasklet(ChatWorker)(sys.stdin, 'console') coio.stackless.schedule_remove(None)
import logging import socket import stackless import sys from syncless import coio from syncless import wsgi from syncless import patch import lprng def WsgiApplication(env, start_response): print >>sys.stderr, 'connection from %(REMOTE_ADDR)s:%(REMOTE_PORT)s' % env start_response("200 OK", [('Content-Type', 'text/html')]) if env['PATH_INFO'] in ('', '/'): return ['<a href="/0">start at 0</a><p>Hello, World!\n'] else: num = int(env['PATH_INFO'][1:]) next_num = lprng.Lprng(num).next() return ['<a href="/%d">continue with %d</a>\n' % (next_num, next_num)] if __name__ == '__main__': logging.root.level = logging.DEBUG patch.patch_stderr() # Give Ctrl-<C> a chance at infinite logging. ss = coio.new_realsocket(socket.AF_INET, socket.SOCK_STREAM) ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ss.bind(('127.0.0.1', 8080)) ss.listen(100) logging.info('listening on %r' % (ss.getsockname(),)) wsgi.WsgiListener(ss, WsgiApplication)
import socket import stackless import sys from syncless import coio from syncless import wsgi from syncless import patch import lprng def WsgiApplication(env, start_response): print >> sys.stderr, 'connection from %(REMOTE_ADDR)s:%(REMOTE_PORT)s' % env start_response("200 OK", [('Content-Type', 'text/html')]) if env['PATH_INFO'] in ('', '/'): return ['<a href="/0">start at 0</a><p>Hello, World!\n'] else: num = int(env['PATH_INFO'][1:]) next_num = lprng.Lprng(num).next() return ['<a href="/%d">continue with %d</a>\n' % (next_num, next_num)] if __name__ == '__main__': logging.root.level = logging.DEBUG patch.patch_stderr() # Give Ctrl-<C> a chance at infinite logging. ss = coio.new_realsocket(socket.AF_INET, socket.SOCK_STREAM) ss.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ss.bind(('127.0.0.1', 8080)) ss.listen(100) logging.info('listening on %r' % (ss.getsockname(), )) wsgi.WsgiListener(ss, WsgiApplication)
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)