简体   繁体   中英

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. 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"

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. But I'm able to have more than one instance / program running, and I don't want that.

I'm using pyinstaller like:

pyinstaller --noconsole -F -i cr.ico mainUsbHidCB05v01.py

I have also tried pyinstaller without the -F option. The result is the same.

Anyone have any idea why with the singleton option in the code the exe doesn't runs??

Thanks.

I had the same problem and I didn't find a way to use the singleinstance of 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. 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.

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. That works like a charm to me. The library is: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM