Sharing common memory space (In form of List) across the python processes.

Piyush Chechani piyush.chechani at tcs.com
Tue Aug 26 09:04:04 EDT 2008


Hi,

Thanks for your reply Terry.

I am still not done with this problem. Please tell me can a server send a 
list object using socket programming to the requesting client?
If yes, how? I am getting the following error 
        "TypeError: send() argument 1 must be string or read-only buffer, 
not list"

For sending list object I tried the following code: - 

Server Code: -
---------------------------------------------------------------------------------------------------------
someList = [ 1, 2, 7, 9, 0 ]
pickledList = pickle.dumps ( id(someList) )

# Our thread class:
class ClientThread ( threading.Thread ):

# Override Thread's __init__ method to accept the parameters needed:
       def __init__ ( self, channel, details ):

          self.channel = channel
          self.details = details
          threading.Thread.__init__ ( self )

       def run ( self ):

          print 'Received connection:', self.details [ 0 ]
          self.channel.send ( pickledList )
 
          self.channel.close()
          print 'Closed connection:', self.details [ 0 ]

# Set up the server:
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )

    # Have the server serve "forever":
while True:
        channel, details = server.accept()
        print channel, details
        ClientThread ( channel, details ).start()


Client Code: -
---------------------------------------------------------------------------------------------------------
class ConnectionThread ( threading.Thread ):

       def run ( self ):

          # Connect to the server:
          client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
          client.connect ( ( 'localhost', 2727 ) )
          #print 'Connected!!',client

          # Retrieve and unpickle the list object:
          lst= pickle.loads ( client.recv ( 1024 ) )

          # Close the connection
          client.close()

# Let's spawn a few threads:
for x in xrange ( 10 ):
       ConnectionThread().start()
---------------------------------------------------------------------------------------------------------

In the above code I am able to get the id of the server List in the 
client, but can not access the content at the client. And if I send the 
pickled list to the client, it creates a new list at the client end, which 
is not desirable.

Please suggest how to share a in-memory list object across two different 
programs?

Thanks.
Piyush.
=====-----=====-----=====
Notice: The information contained in this e-mail
message and/or attachments to it may contain 
confidential or privileged information. If you are 
not the intended recipient, any dissemination, use, 
review, distribution, printing or copying of the 
information contained in this e-mail message 
and/or attachments to it are strictly prohibited. If 
you have received this communication in error, 
please notify us by reply e-mail or telephone and 
immediately and permanently delete the message 
and any attachments. Thank you


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080826/3e904cfb/attachment-0001.html>


More information about the Python-list mailing list