| Deutsch English Français Italiano |
|
<v855da$3stig$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Best (simplest) way to share data
Date: 8 Jul 2024 15:36:57 GMT
Organization: Stefan Ram
Lines: 65
Expires: 1 Feb 2025 11:59:58 GMT
Message-ID: <server-20240708162329@ram.dialup.fu-berlin.de>
References: <9a8nlk-jb81.ln1@q957.zbmc.eu> <v6crrk$1kfb$6@dont-email.me> <294tlk-pqr1.ln1@q957.zbmc.eu>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de EP/XfQQ9F8KRilPfAQov3gE1nzFpcS2K8MulYM2qgWhPO1
Cancel-Lock: sha1:RQOMeO5TYdRU82i5Y2GLHA2UG7A= sha256:L0Iew4kTPsUbseSGpJBh8vG3kXM8vbdISgq+faQhgXc=
X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Bytes: 3442
Chris Green <cl@isbd.net> wrote or quoted:
>That's exactly the sort of solution I was wondering about. Is there a
>ready made module/library for handling this sort of thing? Basically
>it will just be a string of a few tens of characters that would be
>kept up to date by one process and asked for by all the others.
I'm not an expert here, and just quickly tried to make it
run, so the code will still contain errors and not contain
something necessary, but might give you a starting point.
A process doing something (here: printing an incrementing value
named "info") and also serving requests from other processes
for this "info" value:
import asyncio
import socket
class Server:
def __init__( self ):
self.info = 0
async def handle_client( self, reader, writer ):
data = await reader.read( 100 )
message = data.decode()
addr = writer.get_extra_info( 'peername' )
print( f"Received {message!r} from {addr}" )
if message.strip() == "getinfo":
writer.write( str( self.info ).encode() )
await writer.drain()
writer.close()
await writer.wait_closed()
async def print_number( self ):
while True:
print( self.info )
self.info += 1
await asyncio.sleep( 1 )
async def serve( self ):
server = await asyncio.start_server( self.handle_client, '127.0.0.1', 8888 )
addr = server.sockets[0].getsockname()
print( f'Serving on {addr}' )
async with server:
await asyncio.gather( server.serve_forever(), self.print_number() )
asyncio.run( Server().serve() )
, and an example client:
import socket
import time
while True:
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
s.connect( ( '127.0.0.1', 8888 ))
s.send( "getinfo".encode() )
data = s.recv( 100 )
message = data.decode().strip()
print( f"Received: {message}" )
time.sleep( 3 )
.