1

I am having a trouble calling the .send() function of a socket, that was declared at the global level.

from nanomsg import Socket, PAIR, PUB

s2 = Socket(PAIR)
s2.connect('tcp://127.0.0.1:49234')
s2.send(b'connect')

def myfunc():
  global s2
  s2.send(b'Got here!')

myfunc()
print('closing socket')
s2.close()

In the above example the data sent in myfunc() never gets to the receiving socket. I know the receiving socket works because the .send() at the global level is received.

-- addendum --

Just wanted to add that my actual application is a flask-ask web service for a custom Alexa skill and I have verified that myfunc() is getting called by activating the corresponding Alexa custom skill intent.

user3666197
  • 1
  • 6
  • 50
  • 92
  • Are you actually calling `myfunc()` anywhere? – kdopen Aug 01 '17 at 19:34
  • Yes, my actual application is a flask-ask web service for an alexa skill and I have verified that "myfunc" is actually getting called. – Travis Wheatley Aug 01 '17 at 19:58
  • 1
    Hmm... just found that the above code works if I do this as a stand alone script. However, when I try to do the same thing from with a flask-ask web service the call to s2.send() hangs in the function that gets called as a result of triggering the corresponding Alexa custom skill intent. – Travis Wheatley Aug 01 '17 at 20:47
  • `s2.close` doesn't actually close the socket. You need `s2.close()` to do that. – pppery Aug 01 '17 at 20:58
  • Thanks, that's what happens when you fat finger in example code. I'll correct that typo in the example, but still having problems when I do this same thing in a flask-ask web service. – Travis Wheatley Aug 01 '17 at 21:02

1 Answers1

1

You may check if a small time.sleep(0.1) helps. Connecting takes a time and a socket may not be ready for the .send() right away.

    s2 = Socket(PAIR)
    s2.connect('tcp://127.0.0.1:49234')
    time.sleep(0.2)
    s2.send(b'connect')
user3666197
  • 1
  • 6
  • 50
  • 92
sg7
  • 6,108
  • 2
  • 32
  • 40
  • 1
    With all due respect, this was not the O/P problem + he has already confirmed, that the **`"connect"` message got actually delivered** even in the case, when the other `.send()` did not happen to deliver an expected result ( which on the contrary is the issue to help to solve ). – user3666197 Aug 08 '17 at 11:41