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

Piyush Chechani piyush.chechani at tcs.com
Tue Aug 26 08:48:53 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.






Piyush Chechani/DEL/TCS 
08/01/2008 01:24 PM

To
python-list at python.org
cc

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





Hi,

I am working on a module where I need to share contents of a big List 
across the processes. I am using socket programming concept for this.

My current processing for this is as follows: -
        1. There is a server program S which loads the list in the memory, 
and listens on a particular port,
        2. All the other programs which want to access that list sends a 
request on that server port,
        3. Server sends the id of the memory loaded list element to the 
requesting process.
Here I am getting a problem in the reverse function of id(), as my client 
should get the list object using its id but I don't know the python 
function for doing this.

Please help me in solving this problem, also if you have any other more 
robust solution for this processing then please share that.

Thanks in advance.

Piyush.



ForwardSourceID:NT0000B9AE 
=====-----=====-----=====
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/bd0ec259/attachment.html>


More information about the Python-list mailing list