[英]python script with singleton doesn't work after converting it to exe with pyinstaller
I've created my application in python and I want the application to be executed only one at a time.我已经在 python 中创建了我的应用程序,我希望一次只执行一个应用程序。 So I have used the singleton approach:
所以我使用了单例方法:
from math import fmod
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import SIGNAL
import tendo
import pywinusb.hid as hid
import sys
import os
import time
import threading
import UsbHidCB05Connect
if __name__ == "__main__":
just_one = tendo.singleton.SingleInstance()
app = QtGui.QApplication(sys.argv)
screen_UsbHidConnect = ConnectFunction()
screen_UsbHidConnect.show()
sys.exit(app.exec_())
When using the pyinstaller to convert it to an exe, I did not get any error, but when I tried to run the exe I get the error: "Failed to execute script mainUsbHidCB05v01"使用 pyinstaller 将其转换为 exe 时,我没有收到任何错误,但是当我尝试运行该 exe 时,出现错误:“无法执行脚本 mainUsbHidCB05v01”
If in my code I comment the:如果在我的代码中我评论:
import tendo
and和
just_one = tendo.singleton.SingleInstance()
I convert the script to an exe, and the exe runs without any problem.我将脚本转换为 exe,该 exe 运行没有任何问题。 But I'm able to have more than one instance / program running, and I don't want that.
但是我可以运行多个实例/程序,而我不希望那样。
I'm using pyinstaller like:我正在使用 pyinstaller,例如:
pyinstaller --noconsole -F -i cr.ico mainUsbHidCB05v01.py
I have also tried pyinstaller without the -F option.我也试过没有 -F 选项的 pyinstaller。 The result is the same.
结果是一样的。
Anyone have any idea why with the singleton option in the code the exe doesn't runs??任何人都知道为什么在代码中使用单例选项时 exe 不运行?
Thanks.谢谢。
I had the same problem and I didn't find a way to use the singleinstance of tendo.我遇到了同样的问题,我没有找到使用tendo的单一实例的方法。 If You need a solution right now, you can create a file using the "os" library, and put a variable there that when the program is running it is 1, else is 0, so you just have to verify that variable in the beginning of your program.
如果您现在需要一个解决方案,您可以使用“os”库创建一个文件,并在那里放置一个变量,当程序运行时它为 1,否则为 0,因此您只需在开始时验证该变量你的程序。 This is not the best way, but you can use that during the time you need to find a best solution.
这不是最好的方法,但您可以在需要找到最佳解决方案的时候使用它。 :)
:)
Hello, it's me again!
你好,又是我! So, I have found the solution.
所以,我找到了解决方案。 I search a lot and I found very different ways to make the program runs only one time (singleinstance).
我搜索了很多,我发现了让程序只运行一次(单一实例)的非常不同的方法。
In summary, it's possible to use a lock file, using the library OS, but if the computer shutdowns in a energy fall, this file will stay locking your application when it returns, since the app was not closed properly.
总之,使用库操作系统可以使用锁定文件,但是如果计算机因能量下降而关闭,则该文件在返回时将保持锁定您的应用程序,因为应用程序没有正确关闭。 There is other way, when you use TENDO library to create a singleton, there are similar ways for that, but everyone uses some specific DLL, and when you use pyinstaller, adding/importing dll could be a little difficult.
还有另外一种方式,当您使用 TENDO 库创建单例时,也有类似的方式,但是每个人都使用某些特定的 DLL,而当您使用 pyinstaller 时,添加/导入 dll 可能会有些困难。
Finally, there is a third way, that creates a socket communication with the PC that verifies if some specific port is being using for the application.
最后,还有第三种方法,它创建与 PC 的套接字通信,以验证应用程序是否正在使用某个特定端口。 That works like a charm to me.
这对我来说就像一种魅力。 The library is: https://pypi.org/project/Socket-Singleton/
该库是: https : //pypi.org/project/Socket-Singleton/
A simple and workable script:
一个简单可行的脚本:
from time import sleep
from Socket_Singleton import Socket_Singleton
#Socket_Singleton(address="127.0.0.1", port=1337, timeout=0, client=True, strict=True)
Socket_Singleton()
print("hello!")
sleep(10)
print("hello 2!")
I used it with my app and create an .EXE file using pyinstaller and it works very well.
我将它与我的应用程序一起使用并使用 pyinstaller 创建了一个 .EXE 文件,它运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.