import time
start_time = time.time()
--- измеряемый код ----
print("--- %s seconds ---" % (time.time() - start_time))
from multiprocessing import Pool
import time
def doubler(number):
return number * 2
if __name__ == '__main__':
start = time.time()
numbers = [1,2,3,4,5,6,7,8,9]
pool = Pool(processes=9)
print(pool.map(doubler, numbers))
end = time.time()
print(end - start)
collections.deque
A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still.
const sum = arr => arr.reduce((acc, i) => acc + i);
const calculateSquaresSum = arr => sum(arr.map(i => i.calcArea()));
>>> help(Process.join)
join(self, timeout=None)
Wait until child process terminates
from multiprocessing import Process
from time import sleep
def worker(n):
print("the worker went to the office")
sleep(n)
print("the worker has been walking for %d sec" % n)
if __name__ == "__main__":
plist = []
for i in range(4, 0, -1):
proc = Process(target=worker, args=(i,))
plist.append(proc)
for proc in plist:
proc.start()
the worker went to the office
the worker went to the office
the worker went to the office
the worker went to the office
the worker has been walking for 1 sec
the worker has been walking for 2 sec
the worker has been walking for 3 sec
the worker has been walking for 4 sec
www.youtube.com/get_video_info?video_id=XXXXXX
XXXXXX
подставьте хэш видео, который в ссылке на ролик, типа youtu.be/BWCiWZtrWXU
после слеша.url_encoded_fmt_stream_map
. Его значение опять надо распаковать как URL-параметры. И из результата вытащить параметр url
– это ссылка на единый скачивабельный видеофайл.function getUrlParams(search) {
let hashes = search.slice(search.indexOf('?') + 1).split('&')
let params = {}
hashes.map(hash => {
let [key, val] = hash.split('=')
params[key] = decodeURIComponent(val)
})
return params
}
var s = '------'; // здесь длиннющая строка из ответа /get_video_info
var a = getUrlParams(s);
var b = getUrlParams(a.url_encoded_fmt_stream_map);
console.log(b.url); // эту ссылку открываем в браузере - это скачиваемый видеофайл
import asyncio, warnings, itertools
def a():
for ch in 'foo':
print(ch, 'from a')
yield
@asyncio.coroutine
def b():
for ch in 'foo':
print(ch, 'from b')
yield
async def c():
for ch in 'foo':
print(ch, 'from c')
await asyncio.sleep(0)
print('как ни странно, здесь все три прокатывают\n')
loop = asyncio.get_event_loop()
tasks = [loop.create_task(f()) for f in (a, b, c)]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
print('\nмежду тем, есть нюансы\n')
with warnings.catch_warnings():
warnings.simplefilter('ignore')
dirs = [[*dir(f), *dir(f())] for f in (a, b, c)]
for s in sorted(set(itertools.chain(*dirs))):
if not all(s in d for d in dirs):
print(f'{s:14}', *['-+'[s in d] for d in dirs])
Note By default, the __hash__() values of str, bytes and datetime objects are “salted” with an unpredictable random value. Although they remain constant within an individual Python process, they are not predictable between repeated invocations of Python.
This is intended to provide protection against a denial-of-service caused by carefully-chosen inputs that exploit the worst case performance of a dict insertion, O(n^2) complexity. See www.ocert.org/advisories/ocert-2011-003.html for details.
Changing hash values affects the iteration order of dicts, sets and other mappings. Python has never made guarantees about this ordering (and it typically varies between 32-bit and 64-bit builds).
See also PYTHONHASHSEED.
Hashids is a small open-source library that generates short, unique, non-sequential ids from numbers.