A little bit of experience on using celery

Source: Internet
Author: User
Tags rsync

It has been used for half a year. Stability nothing to say.

But there was a pit, which I had not noticed before, and recorded it.

Is that a task can be executed asynchronously in parallel. , but how to need celery execution result to make judgment, it will become asynchronous serial.

This should be worth noting.

For example, the following code implementation, the beginning, I use the result of the results of the judgment to update the database, it becomes serial, can only start a worker at a time,

So, after I put the update database into the same deplay function, I can execute it in parallel.

def rsync_dir (Site_Name, App_name, deploy_version): File_path = "/xxx/autodeploy/" + site_name + "/" + App_name + "/" + deploy_version + "/" Src_file_name = File_count_name (file_path) file_name = Os.path.basename (src_fi Le_name) Md5_value = md5sum (src_file_name) DeployPool.objects.filter (name=deploy_version). Update (md5_value=md5_ Value) Subserver_set = SubServer.objects.filter (app_name__name=app_name) for item in Subserver_set:salt_cmd_ Run.delay (Site_Name, App_name, Deploy_version, Item.id, Md5_value, file_name) @celery_app. Task (TIME_LIMIT=360) def Salt_cmd_run (Site_Name, App_name, Deploy_version, subserver_id, Md5_value, file_name): result = "" item = Subserver. Objects.get (id=subserver_id) TGT = item.server_ip.name Op_name = Item.op_name server_env = Item.server_ip.server_ Env Server_sys = Item.server_ip.server_sys if Server_sys = = "WINDOWS": Rsync_to_path = "D:\\va r\\ops\\autodeploy\\%s\\%s\\%s "% (sitE_name, App_name, deploy_version) else:cmd_str = "RSYNC-AVP--block-size=512--timeout=600--checksum--po rt=4505 "+" rsync://1//prism/%s/%s/%s//var/ops/autodeploy/%s/%s/%s/"% (site_name, App_nam E, Deploy_version, Site_Name, app_name, deploy_version) result = Saltapi_inst (server_env). Cmd_run (TGT, CMD_STR) prin            T result, ' ========rsync result=============== ' if "error" not in str (result): if Server_sys = = "LINUX": Tgt_file_name = "/xxx/autodeploy/%s/%s/%s/%s"% (Site_Name, app_name, deploy_version, file_name) cmd = "MD5 Sum%s "% tgt_file_name result = Saltapi_inst (server_env). Cmd_run (TGT, cmd) if md5_value in Str (resu            LT): Item.rsync_status = True item.save () Else:item.rsync_status = False Item.Save () cmd = "chown-r%s/var/ops"% op_name saltapi_inst (server_env). Cmd_run (TGT, CMD)

  

A little bit of experience on using celery

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.