Python Learning note 4-redis Multi watch for lock inventory

Source: Internet
Author: User

Python has a lot of basic operations on redis, and here are some of the things that people find interesting: 1.redis transactional operations and watch optimistic locking, followed by a description of how to use Redis asynchronously under 2.tornado     Redis is a single-process single-threaded model, itself to the external request is a single task, but also multi-threaded security, which everyone should know, so it is often used Redis do count Services.      first Redis transactions can only use pipeline:in Redis-py MULTI and EXEC can is only used through a pipeline object.http://stackoverflow.com/questions/31769163/what-are-equivalent-functions-of-multi-and-exec-commands-in-redis-pya lot of people like to deal with E-commerce inventory, such as the second time to use Redis to do counter, but the following code to use the transaction to control inventory, is not a problem?
if __name__=="__main__": With Sms_redis.pipeline () as Pipe: while1:            Try:                #Transaction StartPipe.multi () Count= int (sms_redis.get ('Stock_count'))                ifCount > 0:#Available in StockPipe.set ('Stock_count', count-1)                #End of transactionPipe.execute ()#push the order over .                 break            exceptException:traceback.print_exc ()Continue
the problem is that once the Pipe.multi () transaction is executed, the inventory can be locked, which is highlighted here: Redis itself does not have the concept of pessimistic locking, that is, the customer is unable to lock the value of redis, of course, you can be implemented through other paths, such as setnx, here first not talk about;a Redis transaction can be thought of as a one-time return of the batch execution and execution results of a command, there is no problem with the transaction operation itself, the execution process is not interrupted, but the transaction is actually submitted to redis_server at Pipe.execute (). however, It is regrettable that the inventory has the opportunity to be modified by other clients before Redis_server execution, and the concept of lock inventory is completely not available .So how can lock inventory be achieved? Watch is a great solution:  Watch literally is the meaning of monitoring, here can be seen as a database of optimistic lock concept, who can read, who can modify, but the modification of the person must ensure that their watch data has not been modified by others, or the modification failed; 
if __name__=="__main__": With Sms_redis.pipeline () as Pipe: while1:            Try:                #focus on a keyPipe.watch ('Stock_count ')count = int (pipe.get ('Stock_count'))                ifCount > 0:#Available in Stock                    #Transaction Startpipe.multi () Pipe.set ('Stock_count', count-1)                    #End of transactionPipe.execute ()#push the order over .                 break            exceptException:traceback.print_exc ()Continue
If the value is modified after watch, the Pipe.execute () will be reported as abnormal watcherror:watched variable Changed.the Logic of lock inventory based on Redis can be implemented simply.     

Python Learning note 4-redis Multi watch for lock inventory

Related Article

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.