Created
October 29, 2019 09:31
-
-
Save daimon99/0f8587b8060bf832354a76d37ffec79b to your computer and use it in GitHub Desktop.
多线程(多进程)开发
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @main.command() | |
| def query_belong(): | |
| from cdradmin import models as m | |
| from multiprocessing import Pool | |
| mobile_list = m.Mobile.objects.filter(isp__isnull=True).all() | |
| with Pool(8) as p: | |
| p.map(get_belong, mobile_list) | |
| def get_belong(m): | |
| try: | |
| mobile = m.mobile | |
| print('--> ', mobile) | |
| if mobile[:3] == '167': | |
| print('167号段') | |
| return | |
| if m.isp: | |
| print('已存在') | |
| return | |
| ret = requests.get(f'https://abc.com/xxx/?mobile={mobile}').json() | |
| m.isp = ret['data']['isp'] | |
| m.province = ret['data']['province'] | |
| m.city = ret['data']['province'] | |
| lock.acquire() # 加锁原因是 sqlite 不支持并发写 | |
| try: | |
| m.save() | |
| except Exception as e: | |
| print('save error', m, e) | |
| finally: | |
| lock.release() | |
| except Exception as e: | |
| print(m, e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment