Python learning notes (3) and python learning notes

Source: Internet
Author: User
Tags lock queue

Python learning notes (3) and python learning notes

1. in python source code files, if you use non-ASCII characters, you must declare the character encoding in the file header. The declaration is as follows:
# Code: UTF-8


2. Unicode encoding: This is the encoding of All characters in the world. Of course, there is no prescribed storage method .. Therefore, to perform some encoding and conversion, Unicode is usually used as the intermediate encoding. That is, decode the other encoded strings to Unicode, and then encode the strings from the Unicode (encode) into another Encoding


3. The role of decode is to convert other encoded strings to Unicode encoding. For example, name. decode ("GB2312") indicates converting the name of the GB2312 encoded string to Unicode encoding.
? Encode is used to convert Unicode encoding to other encoded strings. For example, name. encode ("GB2312") indicates to convert the name of a GB2312 encoded string to GB2312 encoding.


4. encoding method:
Determines whether the s string is Unicode. If True is returned, False is not returned: isinstance (s, unicode)


5. Obtain the default system encoding:
Import sys
Print sys. getdefaultencoding ()


# Coding: UTF-8

Fp1 = open('test.txt ', 'R ')

Info1 = fp1.read ()

# GBK encoding is known and decoded to Unicode

Tmp = info1.decode ('gbk ')
 

Fp2 = open('test.txt ', 'w ')

# Encode str into a UTF-8

Info2 = tmp. encode ('utf-8 ')

Fp2.write (info2)

Fp2.close ()

 

7. str and unicode object conversion. This sentence is easy to understand.
S = 'tongtong'
Print s. decode ('gbk'). encode ('utf-8 ')

 

8. IndentationError: unexpected indent jin
This makes me miserable. When I used sublime to write Python, I directly wrote and copied it in IDEL, but I did not expect an error, that is, the tab is different from the space.

 


9 There may be three cases from Running to Blocked:
? Synchronization: the synchronization Lock is obtained in the thread, but the resource has been Locked by other threads and enters the Locked State until the resource can be obtained (the acquisition order is controlled by the Lock Queue)

? Sleep: After the thread runs the sleep () or join () method, the thread enters the Sleeping state. The difference is that sleep waits for a fixed period of time, while join waits for the sub-thread to finish running. Of course, you can also specify a "timeout" for join ". In terms of semantics, if two threads a and B call B. join () in a, it is equivalent to merging (join) into a thread. The most common case is to join all sub-threads in the main thread.

? Wait: After the wait () method is executed in the thread, the thread enters the Waiting state and waits for notifications from other threads ).


10? Daemon thread indicates a thread. The daemon thread provides services for other threads, such as JVM garbage collection threads. When all the remaining threads are daemon threads, the process exits.


11. The most common problem in multi-threaded programming: data sharing. When multiple threads modify a shared data, synchronous control is required.


The Lock class is defined in the 12 threading module to facilitate Lock handling:

# Create a lock
Mutex = threading. Lock ()
# Locking
Mutex. acquire ([timeout])
# Release
Mutex. release ()
The locking method acquire can have an optional timeout parameter timeout. If timeout is set, you can use the returned value to determine whether the lock is obtained after the timeout, so that you can perform other processing.

 


13 when a thread calls the lock's acquire () method to obtain the lock, the lock enters the "locked" state. Only one thread can obtain the lock at a time. If another thread tries to obtain the lock at this time, the thread will change to the "blocked" state, called "synchronization blocking"

The lock enters the "unlocked" state after the locks are released by the release () method called by the thread that owns the lock. The thread scheduler selects one of the threads in the synchronous blocking State to obtain the lock and enters the running state.

 

 

14 deadlock: when multiple resources are shared between threads, a deadlock occurs if two threads occupy a part of resources and wait for the resources of the other side at the same time. Although deadlocks rarely occur, once they occur, the application will stop responding.


15 reentrant locks: a simpler deadlock occurs when a thread "iterates" to request the same resource, causing a deadlock: to support multiple requests to the same resource in the same thread, python provides the "reentrant lock": threading. RLock. RLock internally maintains a Lock and a counter variable. counter records the number of acquire times so that resources can be require multiple times. Resources can be obtained only when all acquire of a thread is release. In the above example, if you use RLock instead of Lock, no deadlock will occur:


 

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.