Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Regarding shared memory

Reply
Thread Tools

Regarding shared memory

 
 
gaurav kashyap
Guest
Posts: n/a
 
      10-30-2008
Dear all,

I have a server program that listens to a particular port and a number
of client programs that connect to the server.

Now i want to put some data in form of python list in main memory on
server.Hence whenver a client program is run it connects to the server
and access the data in main memory.Here the server calls a module that
processes the data as per the client request and the returns some
information to the client.

I can create client and server programs using socket programming,but i
am not able to put the data in shared memory and then access it.

NOTE:I want to put the data in main memory only once(on the server
using server program) i.e. whenever client connects to the server it
should only access the data and not create a replica of data already
loaded in memory.How can this be achieved

thanks.
 
Reply With Quote
 
 
 
 
James Mills
Guest
Posts: n/a
 
      10-30-2008
On Thu, Oct 30, 2008 at 2:13 PM, gaurav kashyap <(E-Mail Removed)> wrote:
> Dear all,
>
> I have a server program that listens to a particular port and a number
> of client programs that connect to the server.
>
> Now i want to put some data in form of python list in main memory on
> server.Hence whenver a client program is run it connects to the server
> and access the data in main memory.Here the server calls a module that
> processes the data as per the client request and the returns some
> information to the client.
>
> I can create client and server programs using socket programming,but i
> am not able to put the data in shared memory and then access it.
>
> NOTE:I want to put the data in main memory only once(on the server
> using server program) i.e. whenever client connects to the server it
> should only access the data and not create a replica of data already
> loaded in memory.How can this be achieved


This is trivially done with an event-driven framework
such as Twisted or Circuits. I'm the author of circuits (1.0
release coming soon), so here is a circuits based example:

Server code:
<code>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=3 sts=3 ts=3

"""(Example) Echo Server

A simple Echo Server example that sends back to connected clients
the input the server receieves.

This example demonstrates:
* Basic Component creation.
* Basic Event handling.
* Basic TCP Server

This example makes use of:
* Component
* Event
* Manager
* lib.sockets.TCPServer
"""

from circuits.lib.sockets import TCPServer
from circuits.core import listener, Event, Component, Manager

###
### Components
###

class EchoServer(TCPServer):

def __init__(self, *args, **kwargs):
super(EchoServer, self).__init__(*args, **kwargs)

self.data = [1, 2, 3, 4]

@listener("read")
def onREAD(self, sock, data):
self.write(sock, data)
self.write(sock, "Data: %s" % self.data)

###
### Main
###

def main():
manager = Manager()
server = EchoServer(8000)
manager += server

while True:
try:
manager.flush()
server.poll()
except KeyboardInterrupt:
break

###
### Entry Point
###

if __name__ == "__main__":
main()
</code>

Client code: (I'm just using a basic telnet example here)
<code>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: set sw=3 sts=3 ts=3

"""(Example) Telnet Client

A basic telnet-like clone that connects to remote hosts
via tcp and allows the user to send data to the remote
server.

This example demonstrates:
* Basic Component creation.
* Basic Event handling.
* Basiv Networking
* Basic Request/Response Networking

This example makes use of:
* Component
* Event
* Manager
* lib.sockets.TCPClient
"""

import optparse

from circuits.lib.io import Stdin
from circuits.lib.sockets import TCPClient
from circuits import __version__ as systemVersion
from circuits.core import listener, Event, Component, Manager

USAGE = "%prog [options] host [port]"
VERSION = "%prog v" + systemVersion

###
### Functions
###

def parse_options():
"""parse_options() -> opts, args

Parse any command-line options given returning both
the parsed options and arguments.
"""

parser = optparse.OptionParser(usage=USAGE, version=VERSION)

parser.add_option("-s", "--ssl",
action="store_true", default=False, dest="ssl",
help="Enable Secure Socket Layer (SSL)")

opts, args = parser.parse_args()

if len(args) < 1:
parser.print_help()
raise SystemExit, 1

return opts, args

###
### Components
###

class Telnet(TCPClient):

@listener("connect")
def onCONNECT(self, host, port):
print "Connected to %s" % host

@listener("read")
def onREAD(self, data):
print data.strip()

@listener("stdin:read")
def onINPUT(self, data):
self.write(data)

###
### Main
###

def main():
opts, args = parse_options()

host = args[0]
if len(args) > 1:
port = int(args[1])
else:
port = 23

manager = Manager()

telnet = Telnet()
stdin = Stdin()

manager += stdin
manager += telnet

print "Trying %s..." % host
telnet.open(host, port, ssl=opts.ssl)

while telnet.connected:
try:
manager.flush()
stdin.poll()
telnet.poll()
except KeyboardInterrupt:
break

telnet.close()

###
### Entry Point
###

if __name__ == "__main__":
main()
</code>

Client output:
<paste>
$ ./telnet.py localhost 8000
Trying localhost...
Connected to localhost
test
test
Data: [1, 2, 3, 4]
</paste>

Circuits can be downloaded from:
http://trac.softcircuit.com.au/circuits/

cheers
James

--
--
-- "Problems are solved by method"
 
Reply With Quote
 
 
 
 
Aaron Brady
Guest
Posts: n/a
 
      10-30-2008
On Oct 29, 11:13*pm, gaurav kashyap <(E-Mail Removed)> wrote:
> Dear all,
>
> I have a server program that listens to a particular port and a number
> of client programs that connect to the server.
>
> Now i want to put some data in form of python list in main memory on
> server.Hence whenver a client program is run it connects to the server
> and access the data in main memory.Here the server calls a module that
> processes the data as per the client request and the returns some
> information to the client.
>
> I can create client and server programs using socket programming,but i
> am not able to put the data in shared memory and then access it.
>
> NOTE:I want to put the data in main memory only once(on the server
> using server program) i.e. whenever client connects to the server it
> should only access the data and not create a replica of data already
> loaded in memory.How can this be achieved
>
> thanks.


Shared memory is available in the 'mmap' module. However, you can't
share a Python list object between processes. The closest you can do
is share a ctypes.Array or struct.Struct.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the shm_open() function for shared memory access in linux --regarding gunasingh C Programming 2 04-05-2008 09:36 AM
Avoid memory corruption in shared memory used by several processes? Sune C Programming 14 08-26-2007 03:57 AM
Avoid memory corruption in shared memory used by several processes? Sune C Programming 5 07-13-2007 02:29 PM
regarding heap memory area of a process memory sam_cit@yahoo.co.in C Programming 3 12-17-2006 10:07 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments