Socket logic problem

J

John O'Hagan

I have several instances of the same generator function running
simultaneously, some within the same process, others in separate processes. I
want them to be able to share data (the dictionaries passed to them as
arguments), in such a way that instances designated as "leaders" send their
dictionaries to "follower" instances.

I'm trying to use sockets to relay the dicts in pickled form, like this:

from socket import socket

PORT = 2050
RELAY = socket()
RELAY.bind(('', PORT))
RELAY.listen(5)

PICKLEDICT = ''
while 1:
INSTANCE = RELAY.accept()[0]
STRING = INSTANCE.recv(1024)
if STRING == "?":
INSTANCE.send(PICKLEDICT)
else:
PICKLEDICT = STRING

What I was hoping this would do is allow the leaders to send their dicts to
this socket and the followers to read them from it after sending an initial
"?", and that the same value would be returned for each such query until it
was updated.

But clearly I have a fundamental misconception of sockets, as this logic only
allows a single query per connection, new connections break the old ones, and
a new connection is required to send in a new value.

Are sockets actually the best way to do this? If so, how to set it up to do
what I want? If not, what other approaches could I try?

Regards,

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top