我使用Pool.async执行卸载软件功能。如果没有打包成可执行文件,它工作得很好。但是当我将它打包为exe时,它并没有调用目标函数,而是调用了main函数。这是我的代码。
if __name__ == '__main__':
print("main function called")
try:
# 使用文件锁,限制单进程
parent_folder = "C:\\Program Files (x86)\\ctyun\\eCloudDeskDRT\\sdk"
if not os.path.exists(parent_folder):
os.makedirs(parent_folder)
lock_file_name = 'eCloudDeskDRTSrv.lock'
lock_file_path = os.path.join(parent_folder, lock_file_name)
lock_file_fd = open(lock_file_path, 'w')
portalocker.lock(lock_file_fd, portalocker.LOCK_EX | portalocker.LOCK_NB)
lock_file_fd.writelines(str(os.getpid()))
lock_file_fd.flush()
except BaseException as e:
print("CloudDeskDRTSrv.exe have another instance running.")
sys.exit()
def uninstall_software_sub(software_name, uninstall_string, uninstall_process_name):
try:
log_utils.get_logger().info('uninstall_software_sub')
startupinfo = sp.STARTUPINFO()
startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW
popen = sp.Popen(uninstall_string, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, startupinfo=startupinfo)
log_utils.get_logger().info(popen.pid)
b_cmd = uninstall_string.encode("UTF-8")
log_utils.get_logger().info("execute command %s", uninstall_string)
b_outs, errs = popen.communicate(b_cmd, timeout=None)
if uninstall_process_name:
while windows_utils.check_process_running(uninstall_process_name):
time.sleep(3)
else:
popen.wait()
log_utils.get_logger().info("wait finished")
outs = b_outs.decode('gbk', 'ignore')
if errs:
log_utils.get_logger().error(errs)
else:
log_utils.get_logger().info(outs)
except UnicodeDecodeError as error:
log_utils.get_logger().exception(error)
outs = ""
finally:
if not popen.poll():
popen.kill()
uninstall_software_info = get_uninstall_software_info(software_name)
uninstall_success = uninstall_software_info is None
return uninstall_success
def uninstall_software(software_name, uninstall_callback):
log_utils.get_logger().info("uninstall_software software_name=%s", software_name)
uninstall_info = get_uninstall_software_info(software_name)
uninstall_string = uninstall_info.uninstall_string
if software_name in uninstall_process.keys():
uninstall_process_name = uninstall_process.get(software_name)
log_utils.get_logger().info(os.getpid())
pool = Pool(1)
log_utils.get_logger().info('apply_async')
pool.apply_async(func=third_party_software_check.uninstall_software_sub,
args=(software_name, uninstall_string, uninstall_process_name),
callback=uninstall_callback)不执行unisntall_software_sub,但在执行pool.apply_async时调用main函数。这是日志输出。有人能帮帮我吗?

发布于 2021-08-20 03:04:46
我发现pyinstaller支持多进程。但需要在源代码中添加一些命令。我在main函数下面添加了multiprocessing.freeze_support()。它工作得很好。源代码如下:
if __name__ == '__main__':
print("main function called")
multiprocessing.freeze_support()
try:
# 使用文件锁,限制单进程
parent_folder = "C:\\Program Files (x86)\\ctyun\\eCloudDeskDRT\\sdk"
if not os.path.exists(parent_folder):
os.makedirs(parent_folder)
lock_file_name = 'eCloudDeskDRTSrv.lock'
lock_file_path = os.path.join(parent_folder, lock_file_name)
lock_file_fd = open(lock_file_path, 'w')
portalocker.lock(lock_file_fd, portalocker.LOCK_EX | portalocker.LOCK_NB)
lock_file_fd.writelines(str(os.getpid()))
lock_file_fd.flush()
except BaseException as e:
print("CloudDeskDRTSrv.exe have another instance running.")
sys.exit()https://stackoverflow.com/questions/68841774
复制相似问题