[GH-ISSUE #77] Other serialization format #1033

Closed
opened 2026-03-07 22:04:15 +03:00 by kerem · 11 comments
Owner

Originally created by @hibiken on GitHub (Feb 9, 2020).
Original GitHub issue: https://github.com/hibiken/asynq/issues/77

Originally assigned to: @hibiken on GitHub.

Experiment with other serialization format and benchmark performance and memory usage:

Other serialization format:

  • msgpack
  • protobuf
Originally created by @hibiken on GitHub (Feb 9, 2020). Original GitHub issue: https://github.com/hibiken/asynq/issues/77 Originally assigned to: @hibiken on GitHub. Experiment with other serialization format and benchmark performance and memory usage: Other serialization format: - msgpack - protobuf
kerem 2026-03-07 22:04:15 +03:00
  • closed this issue
  • added the
    idea
    label
Author
Owner

@cielu commented on GitHub (Feb 11, 2020):

Hello, hibiken , how to use this lib in go-gin ?

<!-- gh-comment-id:584473867 --> @cielu commented on GitHub (Feb 11, 2020): Hello, hibiken , how to use this lib in go-gin ?
Author
Owner

@cielu commented on GitHub (Feb 11, 2020):

Why I can't kill the process ?

<!-- gh-comment-id:584480902 --> @cielu commented on GitHub (Feb 11, 2020): Why I can't kill the process ?
Author
Owner

@hibiken commented on GitHub (Feb 11, 2020):

@cielu Which OS are you using?
If you are on Windows, maybe something related to https://golang.org/pkg/os/signal/#hdr-Windows.

Please take a look at the Signal Wiki page on how to gracefully shutdown the background workers.

<!-- gh-comment-id:584482290 --> @hibiken commented on GitHub (Feb 11, 2020): @cielu Which OS are you using? If you are on Windows, maybe something related to https://golang.org/pkg/os/signal/#hdr-Windows. Please take a look at [the Signal Wiki page](https://github.com/hibiken/asynq/wiki/Signals) on how to gracefully shutdown the background workers.
Author
Owner

@cielu commented on GitHub (Feb 11, 2020):

@hibiken macOS, But I start the worker in go-gin . Do you have any good idea ? thanks ..

I'm new at go

<!-- gh-comment-id:584486782 --> @cielu commented on GitHub (Feb 11, 2020): @hibiken macOS, But I start the worker in go-gin . Do you have any good idea ? thanks .. I'm new at go
Author
Owner

@hibiken commented on GitHub (Feb 11, 2020):

So you are using github.com/gin-gonic/gin for your web framework, correct?
And are you starting the workers (bg.Run(handler)) from your web server's main?

<!-- gh-comment-id:584488149 --> @hibiken commented on GitHub (Feb 11, 2020): So you are using [github.com/gin-gonic/gin](https://github.com/gin-gonic/gin) for your web framework, correct? And are you starting the workers (`bg.Run(handler)`) from your web server's main?
Author
Owner

@cielu commented on GitHub (Feb 11, 2020):

@hibiken Yes , that's right . Here is my main func code

func main() {
	// Set Mode
	gin.SetMode(setting.AppCfg.AppMode)
	// Init router
	router := routes.InitRouter()
	// run
	router.Run(":" + setting.AppCfg.AppPost)
	// init asynq worker
	asynqWorker.InitAsynqWorker()
	// init asynq client
	asynqClient.InitAsynqClient()
}

<!-- gh-comment-id:584488661 --> @cielu commented on GitHub (Feb 11, 2020): @hibiken Yes , that's right . Here is my main func code ``` func main() { // Set Mode gin.SetMode(setting.AppCfg.AppMode) // Init router router := routes.InitRouter() // run router.Run(":" + setting.AppCfg.AppPost) // init asynq worker asynqWorker.InitAsynqWorker() // init asynq client asynqClient.InitAsynqClient() } ```
Author
Owner

@hibiken commented on GitHub (Feb 11, 2020):

I see.
The intended use case of asynq is that you use Client to schedule tasks to be processed in the background outside of a user request, and have another process(main.go) to process those tasks in the background.
So you will create another main.go apart from your web server.

So for example, you will have something like this in your web server's main file:

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
        client := asynq.NewClient(/* redis conn opt */)
	r.POST("/signup", func(c *gin.Context) {
                t := asynq.NewTask("send_email", map[string]interface{}{"user_id": 42})
		client.Schedule(t, time.Now())
                // render html or json
	})
	r.Run() 
}

and create a separate main.go file for workers:

func main() {
    r := &asynq.RedisClientOpt{
        Addr: "localhost:6379",
    }

    bg := asynq.NewBackground(r, &asynq.Config{
        Concurrency: 10,
    })

    bg.Run(handler)
}
<!-- gh-comment-id:584490292 --> @hibiken commented on GitHub (Feb 11, 2020): I see. The intended use case of `asynq` is that you use `Client` to schedule tasks to be processed in the background outside of a user request, and have another process(main.go) to process those tasks in the background. So you will create another `main.go` apart from your web server. So for example, you will have something like this in your web server's main file: ```go package main import "github.com/gin-gonic/gin" func main() { r := gin.Default() client := asynq.NewClient(/* redis conn opt */) r.POST("/signup", func(c *gin.Context) { t := asynq.NewTask("send_email", map[string]interface{}{"user_id": 42}) client.Schedule(t, time.Now()) // render html or json }) r.Run() } ``` and create a separate `main.go` file for workers: ```go func main() { r := &asynq.RedisClientOpt{ Addr: "localhost:6379", } bg := asynq.NewBackground(r, &asynq.Config{ Concurrency: 10, }) bg.Run(handler) } ```
Author
Owner

@cielu commented on GitHub (Feb 11, 2020):

Yeah . enhh, I got you . But , can i start the worker in one project ? I don't want separate it .That is a lot of trouble

thanks 😊

<!-- gh-comment-id:584490998 --> @cielu commented on GitHub (Feb 11, 2020): Yeah . enhh, I got you . But , can i start the worker in one project ? I don't want separate it .That is a lot of trouble thanks 😊
Author
Owner

@hibiken commented on GitHub (Feb 11, 2020):

I see.

Background.Run method will block and wait for a termination signal (OS signal TERM or INTERRUPT).

I think if you want to start the workers from your web server's main then try this:

func main() {
	// Set Mode
	gin.SetMode(setting.AppCfg.AppMode)
	// Init router
	router := routes.InitRouter()

        bgDone := make(chan, struct{})

       go func() {
           bg := asynq.NewBackground(r, &asynq.Config{/* options */})
           bg.Run(handler)
           bgDone <- struct{}{}
       }()
	// run
	router.Run(":" + setting.AppCfg.AppPost)
        <-bgDone
}

If that doesn't work, then another option is to fork this repo and export Background.Start and Background.Stop methods (currently they are unexported) and call those to start and stop the background workers, those methods are non-blocking.

<!-- gh-comment-id:584493239 --> @hibiken commented on GitHub (Feb 11, 2020): I see. `Background.Run` method will block and wait for a termination signal (OS signal TERM or INTERRUPT). I think if you want to start the workers from your web server's main then try this: ```go func main() { // Set Mode gin.SetMode(setting.AppCfg.AppMode) // Init router router := routes.InitRouter() bgDone := make(chan, struct{}) go func() { bg := asynq.NewBackground(r, &asynq.Config{/* options */}) bg.Run(handler) bgDone <- struct{}{} }() // run router.Run(":" + setting.AppCfg.AppPost) <-bgDone } ``` If that doesn't work, then another option is to fork this repo and export `Background.Start` and `Background.Stop` methods (currently they are unexported) and call those to start and stop the background workers, those methods are non-blocking.
Author
Owner

@cielu commented on GitHub (Feb 11, 2020):

Ohh. Thank you very much !

<!-- gh-comment-id:584494625 --> @cielu commented on GitHub (Feb 11, 2020): Ohh. Thank you very much !
Author
Owner

@hibiken commented on GitHub (Feb 22, 2020):

Closing this issue for now

<!-- gh-comment-id:589974704 --> @hibiken commented on GitHub (Feb 22, 2020): Closing this issue for now
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/asynq#1033
No description provided.