取消当前运行的功能/goroutine [英] Cancel currently running function/goroutine

查看:56
本文介绍了取消当前运行的功能/goroutine的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gin-gonic 作为HTTP处理程序.我想在用户发出 POST 请求后预渲染一些图形资源.为此,我放置了一个中间件,该中间件将一个函数(内部带有计时器)分配给 map [string] func(),并在分配后直接调用该函数.

I'm using gin-gonic as HTTP handler. I want to prerender some graphical resources after my users make POST request. For this, I put a middleware that assign a function (with a timer inside) to a map[string]func() and call this function directly after assignation.

问题是,当用户发出两个后续请求时,该函数被调用两次.

The problem is, when the user make two subsequent request, the function is called twice.

有什么方法可以清除函数ref和/或他当前正在运行的调用,如Java中的 clearInterval clearTimeout 吗?

Is there any way to clear function ref and/or his currently running call like a clearInterval or clearTimeout in Javascript ?

谢谢

推荐答案

否;您计划作为goroutine运行的任何函数都需要返回或调用 runtime.Goexit .

No; whatever function you've scheduled to run as a goroutine needs to either return or call runtime.Goexit.

如果您正在寻找一种在您的工作程序中建立取消的方法,Go提供了一个处理该问题的原语,它已经是任何HTTP请求(上下文)的一部分.在Go博客中查看以下文章:

If you're looking for a way to build cancellation into your worker, Go provides a primitive to handle that, which is already part of any HTTP request - contexts. Check out these articles from the Go blog:

并发模式:上下文

管道和取消

这篇关于取消当前运行的功能/goroutine的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆