Skip to content

Instantly share code, notes, and snippets.

@daimon99
Created October 29, 2019 09:31
Show Gist options
  • Select an option

  • Save daimon99/0f8587b8060bf832354a76d37ffec79b to your computer and use it in GitHub Desktop.

Select an option

Save daimon99/0f8587b8060bf832354a76d37ffec79b to your computer and use it in GitHub Desktop.
多线程(多进程)开发
@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