Skip to content

Instantly share code, notes, and snippets.

@YT-er
Last active January 15, 2019 06:08
Show Gist options
  • Select an option

  • Save YT-er/cbec185ad5a0711882fbfdd93080141b to your computer and use it in GitHub Desktop.

Select an option

Save YT-er/cbec185ad5a0711882fbfdd93080141b to your computer and use it in GitHub Desktop.
放代码片

代码片

utf头注释

	"utf file header": {
		"prefix": "utf",
		"body": [
		  "# -*- coding: utf-8 -*-",
			"",
			"$0"
		],
		"description": "utf file header"
		},

获取soup对象

	"get soup": {
		"prefix": "gsoup",
		"body": [
			"res = requests.get(url)",
			"res.encoding = 'utf-8'",
			"html = res.text",
			"page = BeautifulSoup(html, 'lxml')",
			"$1"
		],
		"description": "get BeautifulSoup"
	}

多进程+多线程写法 返回值为True则结束所有子进程

    p = Pool(processes=5)
    # 多进程写法1  返回值为True则结束所有子进程,117s
    result = queue.Queue()

    def pool_th():
        for pwd in pwds:
            try:
                result.put(p.apply_async(un_encrypted_rar, args=(rar, pwd)))
            except:
                break

    def result_th():
        while 1:
            a = result.get().get()
            if a:
                p.terminate()
                break
    
    '''
    利用多线程,同事运行P函数创建执行子进程,以及运行获取子进程返回值函数
    '''
    t1 = threading.Thread(target=pool_th)
    t2 = threading.Thread(target=result_th)
    t1.start()
    t2.start()
    t1.join()
    t2.join()


    p.join()

函数时间装饰器

import time

def clock(func):
    def clocked(*args):
        t0 = time.time()
        result = func(*args)
        elapsed = time.time() - t0
        name = func.__name__
        arg_str = ', '.join(repr(arg) for arg in args)
        print(f'[{elapsed:.8f}] {name}({arg_str}) -> {result}')
        return result
    return clocked
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment