Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #70929 > unrolled thread

socket client and server in one application?

Started bychris@freeranger.com
First post2014-05-05 08:33 -0700
Last post2014-05-15 21:59 -0700
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  socket client and server in one application? chris@freeranger.com - 2014-05-05 08:33 -0700
    Re: socket client and server in one application? Marko Rauhamaa <marko@pacujo.net> - 2014-05-05 18:38 +0300
    Re: socket client and server in one application? CHIN Dihedral <dihedral88888@gmail.com> - 2014-05-05 11:26 -0700
    Re: socket client and server in one application? Grant Edwards <invalid@invalid.invalid> - 2014-05-05 19:47 +0000
    Re: socket client and server in one application? chris@freeranger.com - 2014-05-05 15:37 -0700
      Re: socket client and server in one application? Chris Angelico <rosuav@gmail.com> - 2014-05-06 09:05 +1000
        Re: socket client and server in one application? chris@freeranger.com - 2014-05-15 21:59 -0700

#70929 — socket client and server in one application?

Fromchris@freeranger.com
Date2014-05-05 08:33 -0700
Subjectsocket client and server in one application?
Message-ID<2db82378-5bb3-4a5c-b9e0-7ccb29760a06@googlegroups.com>
I have a python script that uses a serial port to read data from an xbee radio and it delivers the data to a socket server.

Now I need to retrieve the data from a socket client so I can send data out on the common serial port.

I think I need a combination of threads, queues, socket client and sever.

Anyone have ideas about how I might frame this out?

Thanks in advance,
Chris.

[toc] | [next] | [standalone]


#70930

FromMarko Rauhamaa <marko@pacujo.net>
Date2014-05-05 18:38 +0300
Message-ID<877g60z153.fsf@elektro.pacujo.net>
In reply to#70929
chris@freeranger.com:

> I think I need a combination of threads, queues, socket client and
> se[r]ver.

Yes, although I'd stay away from threads if I could.

> Anyone have ideas about how I might frame this out?

Take a look at asyncio. It might have everything you could wish for.


Marko

[toc] | [prev] | [next] | [standalone]


#70931

FromCHIN Dihedral <dihedral88888@gmail.com>
Date2014-05-05 11:26 -0700
Message-ID<f383acdf-b5ac-4f68-b145-45de28f6e2f4@googlegroups.com>
In reply to#70929
On Monday, May 5, 2014 11:33:38 PM UTC+8, ch...@freeranger.com wrote:
> I have a python script that uses a serial port to read data from an xbee radio and it delivers the data to a socket server.
> 
> 
> 
> Now I need to retrieve the data from a socket client so I can send data out on the common serial port.
> 
> 
> 
> I think I need a combination of threads, queues, socket client and sever.
> 
> 
> 
> Anyone have ideas about how I might frame this out?
> 
> 
> 
> Thanks in advance,
> 
> Chris.
I did this kind of projects in 1999-2002 for taxie call out services in object pascal from Borland in paid jobs.

In python, check the urlib.


[toc] | [prev] | [next] | [standalone]


#70932

FromGrant Edwards <invalid@invalid.invalid>
Date2014-05-05 19:47 +0000
Message-ID<lk8pro$3mg$1@reader1.panix.com>
In reply to#70929
On 2014-05-05, chris@freeranger.com <chris@freeranger.com> wrote:

> I have a python script that uses a serial port to read data from an
> xbee radio and it delivers the data to a socket server. Now I need to
> retrieve the data from a socket client so I can send data out on the
> common serial port.
>
> I think I need a combination of threads, queues, socket client and
> sever.

You could use either threads or select/poll.  They would all work
fine.

-- 
Grant Edwards               grant.b.edwards        Yow! Now I'm concentrating
                                  at               on a specific tank battle
                              gmail.com            toward the end of World
                                                   War II!

[toc] | [prev] | [next] | [standalone]


#70937

Fromchris@freeranger.com
Date2014-05-05 15:37 -0700
Message-ID<69f1b8e6-0b03-4d26-9b89-0d7b3dc43b16@googlegroups.com>
In reply to#70929
Thanks to Marko, Chin, Grant.

There's a lot to study, asincio, urllib, threads, select/poll.

I'm using a dispatch method to receive and occasionally send data through a serial port on a Raspberry Pi.  I think the dispatch method is essentially a threaded approach, right?

Now to receive serial data and send via socket, then occasionally receive some socket based input and send out on the same serial port.  It's combining the client and server socket code into a single app (so I can have a single connection to the serial port) that has me confused.  I don't see any discussion of that anywhere.

Thanks so much,
Chris.

[toc] | [prev] | [next] | [standalone]


#70938

FromChris Angelico <rosuav@gmail.com>
Date2014-05-06 09:05 +1000
Message-ID<mailman.9689.1399331130.18130.python-list@python.org>
In reply to#70937
On Tue, May 6, 2014 at 8:37 AM,  <chris@freeranger.com> wrote:
> I'm using a dispatch method to receive and occasionally send data through a serial port on a Raspberry Pi.  I think the dispatch method is essentially a threaded approach, right?
>
> Now to receive serial data and send via socket, then occasionally receive some socket based input and send out on the same serial port.  It's combining the client and server socket code into a single app (so I can have a single connection to the serial port) that has me confused.  I don't see any discussion of that anywhere.
>

Threads would be easy. As I understand it, you have two bidirectional
connections, and you're simply linking them? Sounds like a very simple
proxy/tunnel. You don't need to multiplex, so all you need is two
threads: one reading from the socket and writing to the serial port,
and one reading from the serial port and writing to the socket. The
code would look something like this:

serial_port = open(...)
tcp_socket = socket.create_connection(...)
# initialize them both, do whatever setup is needed

def socket_to_serial():
    while True:
        data = tcp_socket.recv(4096)
        serial_port.write(data)

Thread.Thread(target=socket_to_serial).start()

while True:
    data = serial_port.read(4096)
    tcp_socket.send(data)


Two simple loops, running concurrently. Pretty straight-forward as
threads. Both of them will fall idle in their read/recv calls, so
threading works very nicely here.

ChrisA

[toc] | [prev] | [next] | [standalone]


#71639

Fromchris@freeranger.com
Date2014-05-15 21:59 -0700
Message-ID<ec8f10ea-4d32-492b-9003-b6402b2844d8@googlegroups.com>
In reply to#70938
Thank you all.

The 2 threaded in/out sockets are working great.  I'm receiving input on the xbee/serial port  in the dispatch thread.  For each receipt I send the xbee data through a socket connected to node-red.  Node-red is collecting the data, parsing it and sending it along to other end points; mainly to carbon/graphite for charting.

Then I've got some injection nodes in node-red that can send signals back to the python script.  The python script has a socket server in the main loop that waits for a specialized command which, when received, sends a command back out the serial/xbee port to instruct a device to turn on/off.

Generally it works great but I'm tuning it to optimize the amount of data I'm managing.  The xbees are set to send 4 data points every .5 seconds.  I only have 5 xbee radios in my setup but I think that means the little raspi is collecting up to 40 data points every second.

On the node-red, it breaks each data set into 4 discreet entries and sends those to the carbon/graphite charting app.

I'm thinking of including some signal averaging into the python receiver to smooth out the various signals I'm tracking.

Again, thank you all for your help,
Chris.



On Monday, May 5, 2014 4:05:22 PM UTC-7, Chris Angelico wrote:
> On Tue, May 6, 2014 at 8:37 AM,  <chris@freeranger.com> wrote:
> 
> > I'm using a dispatch method to receive and occasionally send data through a serial port on a Raspberry Pi.  I think the dispatch method is essentially a threaded approach, right?
> 
> >
> 
> > Now to receive serial data and send via socket, then occasionally receive some socket based input and send out on the same serial port.  It's combining the client and server socket code into a single app (so I can have a single connection to the serial port) that has me confused.  I don't see any discussion of that anywhere.
> 
> >
> 
> 
> 
> Threads would be easy. As I understand it, you have two bidirectional
> 
> connections, and you're simply linking them? Sounds like a very simple
> 
> proxy/tunnel. You don't need to multiplex, so all you need is two
> 
> threads: one reading from the socket and writing to the serial port,
> 
> and one reading from the serial port and writing to the socket. The
> 
> code would look something like this:
> 
> 
> 
> serial_port = open(...)
> 
> tcp_socket = socket.create_connection(...)
> 
> # initialize them both, do whatever setup is needed
> 
> 
> 
> def socket_to_serial():
> 
>     while True:
> 
>         data = tcp_socket.recv(4096)
> 
>         serial_port.write(data)
> 
> 
> 
> Thread.Thread(target=socket_to_serial).start()
> 
> 
> 
> while True:
> 
>     data = serial_port.read(4096)
> 
>     tcp_socket.send(data)
> 
> 
> 
> 
> 
> Two simple loops, running concurrently. Pretty straight-forward as
> 
> threads. Both of them will fall idle in their read/recv calls, so
> 
> threading works very nicely here.
> 
> 
> 
> ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web