简体   繁体   中英

Maintaining TCP Connection

I have a python program that reads in values from an ADC then writes them to a file as well as send them via TCP if a connection is available. I can send the data fine however as data is constantly being read I would like to be able to keep the connection open. How do I get the client to check that the Server has more data to send and thus to keep the connection open?

This scenario seems very similar to one of our applications. We use ZeroMQ for this.

Using ZeromMQ with PUB/SUB

On PyZMQ doc is example for using Pub/Sub.

Data provider creates PUB socket and sends messages to it.

Data consumer sets up SUB socket and reads messages from.

Typically, PUB socket is fixed part of infrastructure, so it binds to some port, and SUB connects. But if you like, you can switch it and it works too.

Advantages are:

  • provider sends messages to PUB socket and does not block on it
  • reconnects are handled automatically
  • if there is no consumer or connection, PUB socket silently drops the messages.
  • the code to implement this is very short

Other messaging patterns with ZeroMQ

PUB/SUB is just one option, there are other combinations like PUSH/PULL, REQ/REP etc.

I have some services running for years with PUSH/PULL and it stays "quite well" (typically there are 6 weeks before there is a need to do some restart, but this is rather due to problems on hardware than in ZeroMQ library.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM