1

I have this script in Ubuntu 20.04 server, who saves some data I send to it regularly through my laptop, but the script does not run forever as it was supposed to do, even after using a while True loop because when i try to connect to server as usual I get no response and I have to ssh to server again to run it then it work

My code is :

import zmq
import time
import json

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
print("listening on port 5555")
  
while True:
   try:
       message    = socket.recv().decode('UTF-8')
       content    = json.loads(message)
       post_slug  = content['post_slug']
       image_slug = content['image_slug']
       refer      = content['refer']
       links      = content['links']
       result     = f"{content},{post_slug},{image_slug},{refer},{links}"
       socket.send_string(result)
   except Exception as e:
       print(f"could not send data to main server  with error  \n \n {str(e)}")
   time.sleep(1)
user3666197
  • 1
  • 6
  • 50
  • 92
urek mazino
  • 130
  • 12
  • ...what does it do instead? What are the errors you get? In general, what are your observations, rather than just interpretations ("does not run forever")? – Ulrich Eckhardt Dec 03 '21 at 17:07
  • @Ulrich Eckhardt there's no error that the problem i know it stoped when i try to connect to server through port 5555 and i do not get any reply or confirm that it connected – urek mazino Dec 03 '21 at 17:10
  • You can [edit] your question to clarify that. It is better that way than to put various relevant infos into the comments. Also, what is the output it produces? Further, again the same problem the info you provide: "when i try to connect..." -- how *exactly*? Don't let people guess what you're doing! That said, does it even work once? How often and how long does it work? What is the output the script produces, like e.g. error messages? – Ulrich Eckhardt Dec 03 '21 at 17:20

1 Answers1

0

Q :
"... the problem i know it stoped when i try to connect to server through port 5555 and i do not get any reply or confirm that it connected"

A :
Well,
the ZeroMQ REQ/REP-session is not easy to connect / disconnect and re-enter the loop.

Why?
ZeroMQ REQ-side asks ( by a .send() ), and expects the REP-side to indeed answer ( doing first .recv() and sending an answer, using .send() ). In case this distributed-FSA ceases to follow the two-step dance, an unsalvageable mutual-deadlock happens ( sooner or later ... )

As a starting template, this may help solve not to fall into hanging endless on blocking form of the .recv()-method.

...
try:
    print (                     "INF: REP-side is going to .bind()" )
    socket.bind( "tcp://A.B.C.D:5555" )
    print (                     "INF: REP-side was able to .bind()" )

except:
    print (                     "EXC: REP-side on going to .bind()" )
    exit()

...
while True:
   try:
       print (                     "INF: REP-side is going to .poll()" )
       while 0 == socket.poll( timeout = POLL_WAIT_TIME_MSEC,
                               flags   = zmq.POLLIN
                               ):
             print (               "NOP: REP-side is going to .sleep()" )
             time.sleep( POLL_LOOP_SLEEP_TIME )
             print (               "INF: REP-side is going to .poll() again" )

       print (                     "INF: REP-side is ready to .recv()" )

       content    = json.loads( socket.recv().decode( 'UTF-8' ) )

       print (                     "INF: REP-side is recvd a message" )

       post_slug  = content['post_slug']
       image_slug = content['image_slug']
       refer      = content['refer']
       links      = content['links']
       result     = f"{content},{post_slug},{image_slug},{refer},{links}"

       print (                     "INF: REP-side is ready to .send()" )
       socket.send_string( result )

   except Exception as e:
       print (                     "EXC: something happened", repr( e ) )

   time.sleep(1)

Inventory of further problems :

  • receiving side has to cope with being able to handle cases, when (for whatever reasons) the .recv() will face a multipart message in incoming Queue ( the code above silently assumes this will never happen, yet a handling loop shall be added, so as to solve any such case, otherwise one will never be able to keep the flow working - there can never be a .send(), unless all "tail"-frames, from the .recv()-ed, have been all fully .recv()-ed too )

  • configuring the sockets may help make the { .connect() | .disconnect() }-relations from among 1-or-more clients survivable ( .setsockopt( zmq.IMMEDIATE, True ), zmq.CONFLATE, ... and other attributes may help increase connection-level robustness and/or performance ( if needed ) )

  • last but not least, a socket_monitor()-instance is able to provide additional monitoring/signalling level-of-details, if needed to react/respond to connection-level events

user3666197
  • 1
  • 6
  • 50
  • 92
  • after didn't find any solution i made one xd , I created cron job to run the program every 1 minutes , and i made the file check if it already running he'll not executed but if it stopped it's will be executed – urek mazino Jan 12 '22 at 23:35