我尝试使用 PyInstaller 创建可执行文件。该脚本由使用 Gooey 创建的 GUI 和使用 Joblib 创建的并行性组成。当我单独运行代码时,它工作得很好。但是当我将它封装成可执行文件时,它会打开 GUI 次数与工作人员数量一样多,并且不会执行代码。
剧本:
import sys
from gooey import Gooey, GooeyParser
from joblib import Parallel, delayed, parallel_backend
@Gooey(program_name="GUI Example App")
def get_args():
parser = GooeyParser(description="v1")
parser.add_argument(
"text",
metavar="Input Text",
help="Text to be processed",
type=str,
default="",
)
args = parser.parse_args()
return args
def get_ascii(index , c):
c_ascii = ord(c)
print(f"{index}: The ASCII value of given character {c} is {c_ascii}")
def run(args):
print(args.text)
Parallel(n_jobs=-1, verbose=10, prefer='processes')(
delayed(get_ascii)(i, c)
for i, c in enumerate(args.text))
if __name__ == "__main__":
args = get_args()
run(args)
规格文件
import gooey
gooey_root = os.path.dirname(gooey.__file__)
gooey_languages = Tree(os.path.join(gooey_root, 'languages'), prefix = 'gooey/languages')
gooey_images = Tree(os.path.join(gooey_root, 'images'), prefix = 'gooey/images')
a = Analysis(
['parall_gooey.py'],
pathex=[],
binaries=[],
hiddenimports=[],
hookspath=None,
runtime_hooks=None,
)
pyz = PYZ(a.pure)
options = [('u', None, 'OPTION')]
exe = EXE(
pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
gooey_languages, # Add them in to collected files
gooey_images, # Same here.,
name='parall_gooey',
bootloader_ignore_signals=False,
debug=False,
strip=False,
upx=True,
console=False
)
我在网上找了很多解决方案,但都没有解决问题
from multiprocessing import freeze_support
然后在 if __name__ == "__main__":
之后放 freeze_support()
parallel_backend("threading")
:可以工作,但变得非常长。n_core
中Parallel()
的数量。pre_dispatch='1.5 * n_jobs'
在Parallel()
hiddenimports=['joblib']
在规格文件中我目前也面临着同样的问题。您找到任何修复或解决方法吗?
谢谢你