Python threading.Timer object does not work when compiling to .exe

This is a continuation of https://stackoverflow.com/questions/37684111/ironpython-exe-file-closing-immediately-no-exception-thrown

I realized that my program did not work after compilation due to a problem with the Timer object in the thread library. I included the library in the \ Lib \ site-packages directory and added the directory to the path in the program. Here is the test code I use - a simple counting program:

import sys from threading import Timer sys.path.append('C:\Users\[user]\Documents\Visual Studio 2015\Projects\Testing Timer Compilation issue\Testing Timer Compilation issue') sys.path.append('C:\Users\[user]\Documents\Visual Studio 2015\Projects\Testing Timer Compilation issue\Testing Timer Compilation issue\Lib') class Chron(): def __init__(self): self.t = Timer(2, self.count) self.t.start() self.i = 0 def count(self): print(self.i) self.i += 1 if self.i <= 15: self.t = Timer(2, self.count) self.t.start() c = Chron() 

Works great in Interactive Interpreter in Visual Studio, but as soon as I use pyc.py to compile into an exe file, it does not start and just closes after ~ 5 seconds, an exception is not thrown.

As mentioned in the previous question, I have a program with a timer in it that I need to compile, since the source code contains confidential credentials. Are there any tricks needed for the timer to work in exe? Is it just incompatible?

Edit: 6 days unanswered. Unfortunately, there are no resources for this particular problem anywhere on the Internet. It's almost like I'm the only one who has this problem. This seems strange to me, because the problem is related to the Timer object itself, and I cannot imagine that no one else tried to deploy the application with a timer in it. Any insight would be helpful at this point, since I'm completely at a standstill.

+7
python compilation timer ironpython pyc
source share
1 answer

The problem is that you rely on the underlying Python interpreter to gracefully handle the case where the main executable file stream is complete, but there must be some others that still work.

Code execution directly using CPython or IronPython works as expected. The Timer objects you created are actually a specialization of Thread. The interpreter admits that there are some non-demons of the Thread that are still active and therefore do not end. See the docs for an explanation of daemon threads if you do not know the difference between the two types of threads.

However, when you run as executable, it looks like IronPython code uses to wrap the interpreter, it is not very kind. It just waits for the main thread to complete, and then closes everything. This happens even though your Timers are declared non-daemon Threads. Perhaps this is a bug in IronPython.

The solution is to leave your main thread on while the Timer threads are still running. The easiest way to do this for this code example is to just sleep - for example:

 import sys sys.path.append(r"c:\Program Files (x86)\IronPython 2.7\Lib") from threading import Timer from time import sleep class Chron(): def __init__(self): self.t = Timer(2, self.count) self.t.start() self.i = 0 def count(self): print(self.i) self.i += 1 if self.i <= 15: self.t = Timer(2, self.count) self.t.start() c = Chron() sleep(35) 

However, for more complex applications, you should consider some connection between your threads for coordination when you need to close - for example, using join () to wait for the thread to finish.

+5
source share

All Articles