A want to transfer 1000 dollars from your account to B's account. The process of transferring money from a to the end of a transfer is called a transaction. In this transaction, do the following:
1. Subtract 1000 dollars from the account of a. If A's account originally had 3000 dollars, it would now be 2000 dollars.
2. Add 1000 dollars to the account in B. If B's account had 2000 dollars, it would now be 3000 dollars.
If the account of a has been reduced by 1000 dollars, suddenly an accident, such as a power outage or something, causing the transfer transaction unexpectedly terminated, and at this time B's account has not increased by 1000 yuan. Well, we call this operation a failure, to roll back. Rollback is to return to the state before the start of the transaction, that is, back to a account has not been reduced by 1000 blocks of state, B's account of the original state. At this point A's account still has 3000 pieces, B's account still has 2000 blocks.
We have succeeded in either together (a account successfully reduced by 1000, while the B account was successfully increased by 1000), or the operation that failed together (a account returned to its original state and B account back to its original state) was called an atomic operation.
If a transaction can be thought of as a program, it is either completely executed or not executed at all. This property is called atomicity.
The use of [mutex mutex] in Python prevents multiple threads from simultaneously modifying a variable caused by an error.
Mutex = Threading. Lock () Creates a lock that is not locked by default
Mutex.acquire () lock
Mutex.release () Unlock
If a thread executes to mutex.acquire (), if the lock is found to be locked then stop execution until the other thread performs the unlock Operation Mutex.release ()
The principle of locking is that the line of code is as small as possible
The atomic nature of the program, Python to achieve atomicity, lock unlock Threading. Lock ()