[Stackless] Listening to a socket with stacklesslib

Jeff Harper jeff1.61803 at gmail.com
Mon Jul 23 03:55:44 CEST 2012


Hi,

I'm a new subscriber to this list although I have looked through the mail
archives many times over the last few months to find answers to my
questions.

I've recently written a server using stackless and the socket module.  It
was a bit tricky because socket.recv calls are blocking.  So, I created a
"socket thread" that sucked messages from a socket and then used channels
to send the messages to "worker tasklets" running on a different thread.
That way, the "worker tasklets" could run while the "socket thread" was
blocked in socket.recv.

I recently discovered stacklesslib and learned that it has a replacement
for sockets that works well with stackless.  I've tried modifying my server
to use sockets from stacklesslib.  Unfortunately, my server goes into the
socket.accept method but it never returns. The documentation for
stacklesslib is pretty sparse.  I've looked for tutorials on the web and I
haven't found anything.  So, I have no idea what I'm doing wrong

Client and server programs are below.  s.py is a server.  c.py is a
client.  The code is based on section 17.2 in The Python Standard Library.
First, I run s.py:

C:\tmp\remote>s.py
Accepting connections

Then, I run c.py in a different console window.  s.py should then print a
"Connected by" message.  But, nothing happens.  If I modify s.py to avoid
the stackless and stacklesslib stuff by setting GO_STACKLESS = False, it
all works correctly.

I'd be very grateful if someone can tell me what I'm doing wrong or point
me to some tutorials that I've overlooked.  I'm using stacklesslib 1.0.3
and stackless version:

Python 2.7.1 Stackless 3.1b3 060516 (release27-maint, Jan  1 2011,
13:04:37) [MSC v.1500 32 bit (Intel)] on win32


# s.py - This is a server based on the code from the The Python
# Standard Library manual section 17.2

# To use stackless and sockets from stacklesslib set GO_STACKLESS to
# True.  To run the server using standard Python sockets, set
# GO_STACKLESS to False.
GO_STACKLESS = True
# Echo server program
if GO_STACKLESS:
    import stackless
    import stacklesslib.monkeypatch
    stacklesslib.monkeypatch.patch_all()

import socket

def do_server():
    HOST = ''                 # Symbolic name meaning all available
interfaces
    PORT = 50007              # Arbitrary non-privileged port
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    print "Accepting connections"
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        data = conn.recv(1024)
        if not data: break
        conn.send(data)
    conn.close()

if GO_STACKLESS:
    stackless.tasklet(do_server)()
    app = stacklesslib.main.MainLoop()
    app.run()
else:
    do_server()
------------------------------------------------------------

# c.py - This is a server based on the code from the The Python
# Standard Library manual section 17.2.  It connects to s.py, send
# 'Hello, world' then echos a response.

# Echo client program
import socket

HOST = '127.0.0.1'    # The remote host
PORT = 50007              # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.send('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20120722/f3ebaf09/attachment.html>


More information about the Stackless mailing list