I am using Python 3.7.3 on Windows 10 Pro and I need to timeout a program if it gets hung up trying to connect to a printer for too long. Is there a way I can do this without using multiple threads?
Asked
Active
Viewed 45 times
2
-
how are you connection to the printer? usually such client libs have a `timeout` parameter of their own already implemented – Adam.Er8 Jun 24 '19 at 13:43
-
@Adam.Er8 we're using the socket library and connecting over a TCP/IP connection. As far as I know there isn't a build in `timeout` function in anything we are using – Gpsy Jun 24 '19 at 13:46
-
1I believe this should actually work: https://docs.python.org/3/library/socket.html#socket.socket.settimeout – Adam.Er8 Jun 24 '19 at 13:50
-
@Adam.Er8 the issue isn't checking if we can connect to the printer, we have a .jar file that we embedded in the python code and run to get the status of the printer and it really slows things down to do that every time we try to print a label, so we are trying to implement a timeout that only runs that .jar file if the current label doesn't finish printing after a few seconds. – Gpsy Jun 24 '19 at 13:52
-
@Adam.Er8 in other words, the socket connection is not the thing we need to time out – Gpsy Jun 24 '19 at 14:05
-
OK, then I really think the best solution is to use multi-threading, (e.g. https://stackoverflow.com/questions/492519/timeout-on-a-function-call ), I'm not sure if there's a cleaner solution – Adam.Er8 Jun 24 '19 at 14:09
-
@Adam.Er8 I'm running windows, i cant use anything from the 'signal' package – Gpsy Jun 24 '19 at 14:15
-
look at the 2 other answers – Adam.Er8 Jun 24 '19 at 14:19