我在我的应用程序中将 Dropbox 与 celery 结合在一起,这样我就允许用户在连接了他们的 Dropbox 时存储自己的照片。
我写了一段代码,但我担心这可能会导致无限循环,从而杀死系统。
我正在使用的 API 一次只允许 60 张照片,然后它会为您提供分页功能。
这是我的 tasks.py 文件的副本 - 这实际上工作正常,但我想检查我是否在做正确的事情并且不会对系统造成太大影响。
class DropboxUsers(PeriodicTask):
run_every = timedelta(hours=4)
def run(self, **kwargs):
logger = self.get_logger(**kwargs)
logger.info("Collecting Dropbox users")
dropbox_users = UserSocialAuth.objects.filter(provider='dropbox')
for db in dropbox_users:
...
...
...
sync_images.delay(first, second, third_argument)
return True
@task(ignore_result=True)
def sync_images(token, secret, username):
"""docstring for sync_images"""
logger = sync_images.get_logger()
logger.info("Syncing images for %s" % username)
...
...
...
...
feed = api.user_recent_media(user_id='self', count=60)
images = feed[0]
pagination = feed[1]
for obj in images:
### STORE TO DROPBOX
...
...
...
response = dropbox.put_file(f, my_picture, overwrite=True)
### CLOSE DB SESSION
sess.unlink()
if pagination:
store_images.delay(first, second, third, fourth_argument)
@task(ignore_result=True)
def store_images(token, secret, username, max_id):
"""docstring for sync_images"""
logger = store_images.get_logger()
logger.info("Storing images for %s" % username)
...
...
...
...
feed = api.user_recent_media(user_id='self', count=60, max_id=max_id)
images = feed[0]
try:
pagination = feed[1]
except:
pagination = None
for obj in images:
### STORE TO DROPBOX
...
...
...
response = dropbox.put_file(f, my_picture, overwrite=True)
### CLOSE DB SESSION
sess.unlink()
if pagination:
### BASICALLY RESTART THE TASK WITH NEW ARGS
store_images.delay(first, second, third, fourth_argument)
return True
非常感谢您的专业知识。