Jython: socket connecting only once, then Java Exception

Asked by Matteo Acquarone on 2018-10-08

Hello all,
I'm trying to have Sikulix exchange information with a server that have to take some actions before each test, I'm using the below code (well, similar, but this one shows exactly the same problem).
For testing reasons so far the server is on my pc.
At first attempt everything runs well and on the receiving side I see "hello world From SikuliX"
When I try to run again the same script without closing the IDE I have a Java Exception, see below since it is very long.

Closing the IDE and reopening it gives me another successful attempt

I'm not an expert of python, so this may be a stupid problem...in case forgive me....
Thank in advance for your help

This is the code on Sikulix (has some additional and unneeded popup for debug):

import socket
result = Do.popAsk("REadyToconnect?\nclicked within 30 seconds", "testNetWork",30)
if None == result:
  print "nothing to do"
elif result:
    msg="hello world From SikuliX\n"
    data = bytearray( msg)
    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client_socket.connect(("192.168.100.1", 5050))
    client_socket.send(data)
    client_socket.close()
    print "user said yes to\n" + msg
else:
    print "user said no"
exit()

This is the output from Sikulix;
user said yes to
hello world From SikuliX

[info] Exit code: 0

And this is the exception I get at the second attempt:
ott 08, 2018 3:13:59 PM org.python.netty.channel.AbstractChannel$AbstractUnsafe register
WARNING: Force-closing a channel whose registration task was not accepted by an event loop: [id: 0xf7ef9343]
java.util.concurrent.RejectedExecutionException: event executor terminated
at org.python.netty.util.concurrent.SingleThreadEventExecutor.reject(SingleThreadEventExecutor.java:821)
.... more

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
Matteo Acquarone
Solved:
2018-10-09
Last query:
2018-10-09
Last reply:
2018-10-08
Matteo Acquarone (matteoa) said : #1

Another bit of information,
the line that causes the problem at the second run is this one:
client_socket.connect(("192.168.100.1", 5050))
Thanks for any info

RaiMan (raimund-hocke) said : #2

Has apparently nothing to do with SikuliX.

This is either a Jython problem or a usage problem with socket handling.

Since this is run in the IDE, where the state of the interpreter is preserved until the IDE is closed, this might be a state problem of the socket implementation.

Search the net about correct handling of socket communications (your send data, but do not wait for an answer).

Matteo Acquarone (matteoa) said : #3

ok,
I'll go searching the web...the not receiving is intentional, I want first to be able to repeatedly send data and then I'll take care of receiving it...
I tried the same code with Pydev (eclipse) with jython interpreter (without only the pop-up) and it work every time
thanks for your prompt response.
Matteo
p.s. code for PyDev:

import socket
msg="hello world From SikuliX\n"
data = bytearray( msg)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 client_socket.connect(("192.168.100.1", 5050))
client_socket.send(data)
client_socket.close()
exit()

RaiMan (raimund-hocke) said : #4

ok, this is the same as running the script from command line:
with each run a complete new interpreter session is started.

This can be simulated with the SikuiX IDE, by developing the script in the IDE and make it free of syntax errors and then in parallel run the script from command line (after having saved it in the IDE).

If you run it in the IDE, you have to make your code "repeatable",

Matteo Acquarone (matteoa) said : #5

Thanks a lot!
after searching it seems the the netty library could be the cause of the problem, see:
https://stackoverflow.com/questions/32239955/why-does-this-jython-loop-fail-after-a-single-run
I'm not so proficient in Python nor in Jave to give a better answer...

Anyway by using the command line I'm able to "repeat repeatedly" my tests...
Thanks again