golang如何实现静态变量的效果
golang超时等待

任务队列machinery用法

妙音 posted @ 2021年5月01日 09:28 in golang , 1973 阅读
 

 描述

 
go实现的基于消息中间件的异步任务队列, 下面是学习笔记
 

使用概述

 
步骤1: 创建server,配置参数、注册task。(此处server只是个配置作用, 并不是单独的server进程)
 
步骤2: 启动worker
 
步骤3: 发送task
 
与celery的用法是完全一致的
 
 

创建server

 
func startServer() (*machinery.Server, error) {
cnf := &config.Config{
Broker:          "amqp://guest:guest@localhost:5672/",
DefaultQueue:    "machinery_tasks",
ResultBackend:   "amqp://guest:guest@localhost:5672/",
ResultsExpireIn: 3600,  //任务有效期
AMQP: &config.AMQPConfig{
Exchange:      "machinery_exchange",
ExchangeType:  "direct",
BindingKey:    "machinery_task",
PrefetchCount: 3,   //限定消费能力
},
}

// Create server instance
broker := amqpbroker.New(cnf)
backend := amqpbackend.New(cnf)
lock := eagerlock.New()     //任务锁
server := machinery.NewServer(cnf, broker, backend, lock)

// Register tasks
tasks := map[string]interface{}{
"add":               exampletasks.Add,
"multiply":          exampletasks.Multiply,
"sum_ints":          exampletasks.SumInts,
"sum_floats":        exampletasks.SumFloats,
"concat":            exampletasks.Concat,
"split":             exampletasks.Split,
"panic_task":        exampletasks.PanicTask,
"long_running_task": exampletasks.LongRunningTask,
}

return server, server.RegisterTasks(tasks)
}
 

创建worker

 
创建worker, 之后就可以启动了
 
func worker() error {
    //消费者的标记
consumerTag := "machinery_worker"

server, err := startServer()
if err != nil {
return err
}

    //第二个参数并发数, 0表示不限制
worker := server.NewWorker(consumerTag, 0)

    //钩子函数
errorhandler := func(err error) {}
pretaskhandler := func(signature *tasks.Signature) {}
posttaskhandler := func(signature *tasks.Signature) {}

worker.SetPostTaskHandler(posttaskhandler)
worker.SetErrorHandler(errorhandler)
worker.SetPreTaskHandler(pretaskhandler)
return worker.Launch()
}
 
启动结果
 
INFO: 2021/05/01 08:28:27 worker.go:58 Launching a worker with the following settings:
INFO: 2021/05/01 08:28:27 worker.go:59 - Broker: amqp://192.168.120.101:5672
INFO: 2021/05/01 08:28:27 worker.go:61 - DefaultQueue: machinery_tasks
INFO: 2021/05/01 08:28:27 worker.go:65 - ResultBackend: amqp://192.168.120.101:5672
INFO: 2021/05/01 08:28:27 worker.go:67 - AMQP: machinery_exchange
INFO: 2021/05/01 08:28:27 worker.go:68   - Exchange: machinery_exchange
INFO: 2021/05/01 08:28:27 worker.go:69   - ExchangeType: direct
INFO: 2021/05/01 08:28:27 worker.go:70   - BindingKey: machinery_task
INFO: 2021/05/01 08:28:27 worker.go:71   - PrefetchCount: 0
INFO: 2021/05/01 08:28:27 amqp.go:96 [*] Waiting for messages. To exit press CTRL+C
 

发送任务

 
server, _ := startServer()

signature := &tasks.Signature{
    Name: "add",
    Args: []tasks.Arg{
        {
            Type:  "int64",
            Value: 1,
        },
        {
            Type:  "int64",
            Value: 1,
        },
    },
}
asyncResult, _ := server.SendTask(signature)
fmt.Println(asyncResult.Get(time.Millisecond * 5))  //等待间隔,理论上是越小越好

//asyncResult.GetWithTimeout(time.Second*120, time.Millisecond * 5)   //第一个参数才是timeout
 

总结

 
以上就是machinery的基本用法,与celery基本一样, 更详细内容参考官方文档
 
 
 
Avatar_small
Imamia Quran Academy 说:
2023年7月23日 06:03

Nice blog. I enjoyed reading your articles. This is truly a great read for me. I am from Imamia Quran Academy. Our Online Shia Quran Academy is a comprehensive institution with full fledged facilities regarding Shia Quran Learning. Shia Online Learning more efficiently saves time and money spent over conveyance and physical going to any conventional Shia Quran Institute.

 

Avatar_small
안전토토사이트 说:
2023年8月02日 03:50

I know your aptitude on this. I should say we ought to have an online discourse on this. Composing just remarks will close the talk straight away! What's more, will confine the advantages from this data.

Avatar_small
Imamia Quran Academy 说:
2024年1月18日 08:30

Online Shia Quran Academy is an institute for Quran Education. They have trained teaching staff along with renowned Islamic Scholars for a better understanding of the Quran. Shia Quran Academy is an Online Ahle Tashi Institute that has all the features of traditional madrasahs including the teaching of an authentic understanding of the Quran. Additionally, Shia Quran Academy Online provides all teaching by certified professional teachers. Thus, joining Quran Academy will be suitable and beneficial for every learner. It is very difficult to find a good Online Shia Quran Center with Shia Teachers abroad except in Muslim or especially Ahle Tashi majority countries. Imamia Quran Center connects students virtually from all over the world including countries with less or no Ahle Tashi population or Shia Quran Centers. Our Quran Academy is a comprehensive institution with full fledged facilities regarding Shia Quran Learning. Ahle Tashi Quran student is free to choose the timings of classes, lecturers and tutors according to their feasibility and availability to Learn Shia Quran. Online learning more efficiently saves time and money spent over conveyance and physical going to any conventional Shia Quran Institute. Online Shia Madrasa selects highly qualified Quran teachers, keeping in mind the ages of kids and adults, who teach kids and adults Quranic education as well as their beliefs, spirituality and worldly life. The Shia Quran Madrasa Online is a secure place for kids and adults to learn and memorize the Quran. They teach the translation and interpretation of the Holy Quran under the guidance of expert Quranic teachers. Our Shia Quran Madrasa is second to none in providing education to Ahle Tashi women’s. So our center has selected Ahle Tashi Female Quran teacher with higher education from Madrasas. Online Shia Quran Madrasa is one of the best academies providing its services all over the world including USA, UK, Canada, Germany, France and Australia. Shia Quran Academy, a leading Online Shia Academy in the USA, offers exceptional virtual services that are accessible to anyone worldwide.

Avatar_small
Top SEO 说:
2024年3月29日 16:47

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too. mas informacion

Avatar_small
Top SEO 说:
2024年4月01日 14:46 I read that Post and got it fine and informative. Please share more like that... online nursing assignment help in australia
Avatar_small
Top SEO 说:
2024年4月01日 14:58

The article posted was very informative and useful. You people are doing a great job. Keep going. istana777

Avatar_small
Top SEO 说:
2024年4月04日 01:48 it's really cool blog. Linking is very useful thing.you have really helped lock 101
Avatar_small
Top SEO 说:
2024年4月05日 15:07

Hi there, I found your blog via Google while searching for such kinda informative post and your post looks very interesting for me pijat panggilan gading serpong

Avatar_small
Top SEO 说:
2024年4月07日 14:30

Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! Denticore

Avatar_small
Top SEO 说:
2024年4月07日 14:31

I am a new user of this site so here i saw multiple articles and posts posted by this site,I curious more interest in some of them hope you will give more information on this topics in your next articles. Free SHEIN Voucher Codes

Avatar_small
Top SEO 说:
2024年4月07日 14:33

Piala88 adalah link situs judi slot online gacor maxwin terbaik dan terpercaya 2024 Situs Slot Online

Avatar_small
sa 说:
2024年4月09日 00:34 I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! Over 3.5 goals
Avatar_small
sa 说:
2024年4月09日 00:37

I found that site very usefull and this survey is very cirious, I ' ve never seen a blog that demand a survey for this actions, very curious... 666 correct score

Avatar_small
sa 说:
2024年4月09日 00:42

Very good points you wrote here..Great stuff...I think you've made some truly interesting points.Keep up the good work. statarea

Avatar_small
Top SEO 说:
2024年4月10日 04:00

Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine. jual toto

Avatar_small
Top SEO 说:
2024年4月10日 04:05

This was incredibly an exquisite implementation of your ideas slot

Avatar_small
Top SEO 说:
2024年4月12日 17:38 Very informative post! There is a lot of information here that can help any business get started with a successful social networking campaign. free trial vps no credit card
Avatar_small
Top SEO 说:
2024年4月13日 15:47

Very awesome!!! When I seek for this I found this website at the top of all blogs in search engine. oesv four roses

Avatar_small
sa 说:
2024年4月15日 15:31

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. Lowongan Kerja Terbaru

Avatar_small
sa 说:
2024年4月15日 17:41

Great article Lot's of information to Read...Great Man Keep Posting and update to People..Thanks Animeindo

Avatar_small
sa 说:
2024年4月15日 17:44

Very nice article, I enjoyed reading your post, very nice share, I want to twit this to my followers. Thanks!. บ้านซีรี่ย์

Avatar_small
sa 说:
2024年4月15日 17:47

I really thank you for the valuable info on this great subject and look forward to more great posts Samehadaku

Avatar_small
sa 说:
2024年4月15日 17:51

Hi! This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job! DramaQu

Avatar_small
sa 说:
2024年4月15日 17:54

This is such a great resource that you are providing and you give it away for free. Drakor id

Avatar_small
sa 说:
2024年4月15日 17:58

Hi, I log on to your new stuff like every week. Your humoristic style is witty, keep it up Drakorindo

Avatar_small
sa 说:
2024年4月15日 18:03

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. โอเวอร์ ซีรี่ย์

Avatar_small
sa 说:
2024年4月15日 18:06

This blog is so nice to me. I will keep on coming here again and again. Visit my link as well.. KissAnime

Avatar_small
sa 说:
2024年4月15日 18:10

Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. AnimeKisa

Avatar_small
sa 说:
2024年4月19日 05:48

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Affordable Wallpaper Installation UAE

Avatar_small
sa 说:
2024年4月19日 05:54

Thanks for the best blog. it was very useful for me.keep sharing such ideas in the future as well. upholstery cleaning newcastle

Avatar_small
sa 说:
2024年4月20日 14:29

Its as if you had a great grasp on the subject matter, but you forgot to include your readers. Perhaps you should think about this from more than one angle. pijat panggilan karawaci

Avatar_small
sa 说:
2024年4月20日 14:35

If you are looking for more information about flat rate locksmith Las Vegas check that right away. kampung inggris semarang

Avatar_small
sa 说:
2024年4月20日 19:12 The article posted was very informative and useful. You people are doing a great job. Keep going. windows 12 iso file download
Avatar_small
sa 说:
2024年4月23日 03:23

Very efficiently written information. It will be beneficial to anybody who utilizes it, including me. Keep up the good work. For sure i will check out more posts. This site seems to get a good amount of visitors. Vivamax Sub Indo

Avatar_small
sa 说:
2024年4月24日 21:30 I am hoping the same best effort from you in the future as well. In fact your creative writing skills has inspired me. pahala4d
Avatar_small
sa 说:
2024年4月24日 21:36

Wow i can say that this is another great article as expected of this blog.Bookmarked this site.. pahala4d

Avatar_small
Top SEO 说:
2024年4月25日 13:37 Very informative post ! There is a lot of information here that can help any business get started with a successful social networking campaign ! etrebelle
Avatar_small
sa 说:
2024年4月26日 02:55 Эксклюзивные турниры только на VAVADA. Приветственный бонус до 1 000 $ + 100 фриспинов. Играйте на фишки, а выигрывайте реальные деньги. вавада официальный сайт
Avatar_small
sa 说:
2024年4月27日 14:19 This is truly an practical and pleasant information for all. Thanks for sharing this to us and more power slotebi
Avatar_small
sa 说:
2024年4月28日 15:09 I read a article under the same title some time ago, but this articles quality is much, much better. How you do this.. Explosion 2
Avatar_small
sa 说:
2024年4月28日 15:19

This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post. agen slot terpecaya

Avatar_small
sa 说:
2024年4月30日 01:31 This is also a very good post which I really enjoy reading. It is not everyday that I have the possibility to see something like this. Design de site WordPress
Avatar_small
sa 说:
2024年4月30日 01:41

I think about it is most required for making more on this get engaged streaming kelas bintang

Avatar_small
sa 说:
2024年5月01日 18:29 What a really awesome post this is. Truly, one of the best posts I've ever witnessed to see in my whole life. Wow, just keep it up. jual semen mortar
Avatar_small
Top SEO 说:
2024年5月02日 19:31

If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state. myfreelance.ma/maintenance-informatique-infogerance-meknes-fes réparation windows office antivirus réseaux imprimantes vpn rdp access distant serveurs ordinateurs pc fibre optique wifi internet agence site web création portable firewall

Avatar_small
sa 说:
2024年5月02日 20:52 Wow! Such an amazing and helpful post this is. I really really love it. It's so good and so awesome. I am just amazed. I hope that you continue to do your work like this in the future also. sewa mobil di bali lepas kunci
Avatar_small
Top SEO 说:
2024年5月02日 21:42

Two full thumbs up for this magneficent article of yours. I've really enjoyed reading this article today and I think this might be one of the best article that I've read yet. Please, keep this work going on in the same quality. LEAKS FORUM

Avatar_small
Top SEO 说:
2024年5月06日 08:52

Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing. seo maroc casablanca community manager agence réseaux gestion sociaux Facebook Ads Instagram Google adwords youtube snapshot Rabat Tanger Marrakech Agadir Meknès Fès

Avatar_small
Top SEO 说:
2024年5月07日 00:18 Great post i must say and thanks for the information. Education is definitely a sticky subject. However, is still among the leading topics of our time. I appreciate your post and look forward to more. erp maroc crm rabat casablancamarrakech agadir tanger fes meknes
Avatar_small
Top SEO 说:
2024年5月08日 01:53

I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done! chocolat chocolatier chocolaterie rabat casablanca maroc tanger fes meknes

Avatar_small
pahala4d 说:
2024年5月21日 06:31

<a href="https://businessopenexchange.com">pahala4d</a> ~ Situs Online Terbaik Dengan Via Qris Tercepat Di Tahun 2024

Avatar_small
pahala4d 说:
2024年5月29日 06:35

[url=https://mworkz.net]pahala4d[/url]"Top Login Games Online Fast Super Jackpot

Avatar_small
pahala4d 说:
2024年5月29日 20:38

PAHALA4D - Platform Terlengkap Dengan Game Terbaik 2024

Avatar_small
pahala4d 说:
2024年5月29日 20:38

<a href="https://pahalabest.com">PAHALA4D</a> - Platform Terlengkap Dengan Game Terbaik 2024

Avatar_small
pahala4d 说:
2024年6月09日 07:34

PAHALA4D | Situs Pay4d Gacor Dengan Deposit Qris Dan Server Pg Soft 2024

Avatar_small
gwinbola 说:
2024年6月09日 07:34

GWINBOLA ~ Situs Idn Deposit Pulsa Tanpa Potongan Dengan Login Cepat 2024

Avatar_small
pahala4d 说:
2024年6月27日 06:41

PAHALA4D : Situs Rekomendasi Yang Terpilih Di Asia Pasti Win Besar

Avatar_small
pahala4d 说:
2024年7月04日 05:34

PAHALA4D - Link Alternatif pahala4d .com Paling Bonafit Di Tahun 2024

Avatar_small
Pahala4d 说:
2024年12月09日 17:14

<a href="https://heylink.me/LOGINPAHALA4D">pahala4d</a> - Situs Online Yang Paling Gacor Tahun 2024

Avatar_small
Pahala4d 说:
2024年12月10日 05:02

<a href="https://heylink.me/Pahala4Dtoto">pahala4d</a>
PAHALA4D

Avatar_small
Pahala4d 说:
2024年12月10日 05:13

<a href="https://heylink.me/Pahala4Dtoto">pahala4d</a>
PAHALA4D

Avatar_small
Pahala4d 说:
2024年12月26日 04:19

<a href="https://pahala4d89.vzy.io">pahala4d</a>
PAHALA4D Situs Resmi Amanah Dan Terpercaya 2025

Avatar_small
Pahala4d 说:
2024年12月26日 07:56

PAHALA4D Situs Resmi Amanah Dan Terpercaya 2025

Avatar_small
Pahala4d 说:
2024年12月26日 07:56

https://pahala4d89.vzy.io


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter