list: Lists are dynamic arrays that let us modify and resize the data we are storing, tuple: tuples are static arrays whose contents are fixed and immutable. 同属于 arrays
Lists are dynamic arrays; they are mutable and allow for resizing (changing the number of elements that are held).
Tuples are static arrays; they are immutable, and the data within them cannot be changed after they have been created.
Tuples are cached by the Python runtime, which means that we don’t need to talk to the kernel to reserve memory every time we want to use one.
def index_sequence(key, mask=0b111, PERTURB_SHIFT=5):
perturb = hash(key) 1
i = perturb & mask
yield i
while True:
perturb >>= PERTURB_SHIFT
i = (i * 5 + perturb + 1) & mask
yield i
By default, the smallest size of a dictionary or set is 8,it will resize by 3× if the dictionary is more than two-thirds full
8; 18; 39; 81; 165; 333; 669; 1,341; 2,685; 5,373; 10,749; 21,501; 43,005;
__hash__ with __cmp__ or __eq__
local() -> globals() -> \_\_builtin\_\_
List Dict
# The Python loop
for i in object:
do_work(i)
# Is equivalent to
object_iterator = iter(object)
while True:
try:
i = next(object_iterator)
except StopIteration:
break
else:
do_work(i)
islice
Allows slicing a potentially infinite generator
chain
Chains together multiple generators
takewhile
Adds a condition that will end a generator
cycle
Makes a finite generator infinite by constantly repeating it
await Future object contain a result
hard drive, network adapter, GPU
Usage | serial | gevent | tornado | aiohttp |
---|---|---|---|---|
Runtime (s) | 102.684 | 1.142 | 1.171 | 1.101 |
Process A forked copy of the current process; this creates a new process identifier, and the task runs as an independent child process in the operating system. You can start and query the state of the Process and provide it with a target method to run.
Pool Wraps the Process or threading.Thread API into a convenient pool of workers that share a chunk of work and return an aggregated result.
Queue A FIFO queue allowing multiple producers and consumers.
Pipe A uni- or bidirectional communication channel between two processes.
Manager A high-level managed interface to share Python objects between processes.
ctypes Allows sharing of primitive datatypes (e.g., integers, floats, and bytes) between processes after they have forked.
multiprocessing use physical cores
behave similarly to array.array
seek read_byte write_byte
#Clusters and Job Queues
produce -> topic -> channels -> consumers
start an instance
curl "http://127.0.0.1:4151/stats"
or
nsqadmin --lookupd-http-address=127.0.0.1:4161
nsq_tail --topic prime -n 5 --nsqd-tcp-address=127.0.0.1:4150
int object costs 24 bytes in Python 3.7
empty byte sequence costs 33 bytes
An empty list costs 64 bytes, each item in the list takes another 8 bytes on a 64-bit laptop
self.numbits = int((-capacity * math.log(error)) // math.log(2) ** 2 + 1) self.numhashes = int((self.num_bits * math.log(2)) // capacity + 1)