def _handle_internal(self, params):
        response = {'submissions': []}
        ids = set()
        with self.conn.begin():
            source_id = find_or_insert_source(self.conn, params.application_id, params.account_id, params.application_version)
            format_ids = {}
            for p in params.submissions:
                if p['format']:
                    if format_ids not in p['format']:
                        format_ids[p['format']] = find_or_insert_format(self.conn, p['format'])
                    p['format_id'] = format_ids[p['format']]
            for p in params.submissions:
                mbids = p['mbids'] or [None]
                for mbid in mbids:
                    values = {
                        'mbid': mbid or None,
                        'puid': p['puid'] or None,
                        'bitrate': p['bitrate'] or None,
                        'fingerprint': p['fingerprint'],
                        'length': p['duration'],
                        'format_id': p.get('format_id'),
                        'source_id': source_id
                    }
                    meta_values = dict((n, p[n] or None) for n in self.meta_fields)
                    if any(meta_values.itervalues()):
                        values['meta_id'] = insert_meta(self.conn, meta_values)
                    if p['foreignid']:
                        values['foreignid_id'] = find_or_insert_foreignid(self.conn, p['foreignid'])
                    id = insert_submission(self.conn, values)
                    ids.add(id)
                    submission = {'id': id, 'status': 'pending'}
                    if p['index']:
                        submission['index'] = p['index']
                    response['submissions'].append(submission)

        if self.redis is not None:
            self.redis.publish('channel.submissions', json.dumps(list(ids)))

        clients_waiting_key = 'submission.waiting'
        clients_waiting = self.redis.incr(clients_waiting_key) - 1
        try:
            max_wait = 10
            self.redis.expire(clients_waiting_key, max_wait)
            tracks = {}
            remaining = min(max(0, max_wait - 2 ** clients_waiting), params.wait)
            logger.debug('starting to wait at %f %d', remaining, clients_waiting)
            while remaining > 0 and ids:
                logger.debug('waiting %f seconds', remaining)
                time.sleep(0.5) # XXX replace with LISTEN/NOTIFY
                remaining -= 0.5
                tracks = lookup_submission_status(self.conn, ids)
                if not tracks:
                    continue
                for submission in response['submissions']:
                    id = submission['id']
                    track_gid = tracks.get(id)
                    if track_gid is not None:
                        submission['status'] = 'imported'
                        submission['result'] = {'id': track_gid}
                        ids.remove(id)
        finally:
            self.redis.decr(clients_waiting_key)

        return response
dataset/ETHPy150Open lalinsky/acoustid-server/acoustid/api/v2/__init__.py/SubmitHandler._handle_internal/swapped operands of `not in`