开发实例

继续执行主线程 #

from concurrent.futures import ThreadPoolExecutor

# 使用 ThreadPoolExecutor 创建线程池
executor = ThreadPoolExecutor(max_workers=len(models) + 1)

# 提交生产者任务到线程池
executor.submit(
    DetectionService._producer_frames,
    read_data,
    mode,
    interval_seconds,
    start_time,
    end_time,
    queue_manager
)

# 提交消费者任务到线程池
for key, value in CV_MODEL.items():
    with queue_manager.lock:
        q = queue_manager.queues.get(key)
    executor.submit(
        DetectionService._consumer_frames,
        q, mode, diff, name, key, value
    )

主线程等待 #

from concurrent.futures import ThreadPoolExecutor

# 使用 ThreadPoolExecutor 创建线程池,并设置最大工作线程数为 4
with ThreadPoolExecutor(max_workers=4) as executor:
    # 提交生产者任务到线程池
    executor.submit(
        DetectionService._producer_frames,
        read_data,
        mode,
        interval_seconds,
        start_time,
        end_time,
        queue_manager
    )
    
    # 提交消费者任务到线程池
    for key, value in CV_MODEL.items():
        with queue_manager.lock:
            q = queue_manager.queues.get(key)
        executor.submit(
            DetectionService._consumer_frames,
            q, mode, diff, name, key, value
        )

go启动Python插件 #

python打包成可执行文件,run.py及其他文件

mainPyPath := filepath.Join(appDir, "api", "gemain.py")
			args := []string{
				"-E", mainPyPath,
				"--cid", fmt.Sprintf("%d", cid),
				"--carid", fmt.Sprintf("%d", carid),
				"--eid", fmt.Sprintf("%d", eid),
				"--tid", fmt.Sprintf("%d", tid),
				"--appName", plugin.AppName,
				"--host", global.MastHost(),
				"--plugin", plugin.PluginExePath,
			}
			common.Log.Info("args:", args)

			common.Log.Info("m.pythonPath:", m.pythonPath)
			// common.Log.Info("args:", args)
			if strings.Contains(plugin.AppName, "edr") {
				env = append(env, fmt.Sprintf("PATH=%s", filepath.Join(common.GetExecDir(), "bin", "zlgcan_x64")))
				common.Log.Info(env)
			}
			worker.process, err = StartPythonPlugin(m.pythonPath, nil, env, args...)
func StartPythonPlugin(pythonPath string, output io.Writer, env []string, args ...string) (*os.Process, error) {
	cmd := exec.Command(pythonPath, args...)
	cmd.Dir = filepath.Dir(pythonPath)
	cmd.Env = GetPythonEnv()
	cmd.Env = append(cmd.Env, env...)
	// common.Log.Info(cmd.Env)
	logDir := common.GetAppdata()
	if logDir == "" {
		logDir = common.GetExecDir()
	}
	if output == nil {
		manageCmdPath := filepath.Join(logDir, global.AppName, "log", fmt.Sprintf("%dPyProcess.log", app.LogProcess))
		if !common.IsDirExist(filepath.Dir(manageCmdPath)) {
			os.MkdirAll(filepath.Dir(manageCmdPath), os.ModePerm)
		}
		info, err := os.Stat(manageCmdPath)
		if err == nil {
			if info.Size() >= 1024*1024*50 {
				os.Remove(manageCmdPath)
			}
		}

		file, err := os.OpenFile(manageCmdPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm)
		if err != nil {
			common.Log.Error("OpenFile err:", err)
			cmd.Stdout = os.Stdout
		} else {
			cmd.Stdout = file
			cmd.Stderr = file
		}
	} else {
		cmd.Stdout = output
		cmd.Stderr = output
	}

	err := cmd.Start()
	if err != nil {
		return nil, err
	}

	if cmd.Process != nil {
		return cmd.Process, nil
	}
	return nil, fmt.Errorf("%+v 启动失败", args)
}