<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _get_pgtiou(pgt):
 """ Returns a PgtIOU object given a pgt. The PgtIOU (tgt) is set by the CAS server in a different request that has completed before this call, however, it may not be found in the database by this calling thread, hence the attempt to get the ticket is retried for up to 5 seconds. This should be handled some better way. Users can opt out of this waiting period by setting CAS_PGT_FETCH_WAIT = False :param: pgt """


    pgtIou = None
    retries_left = 5

    if not settings.CAS_PGT_FETCH_WAIT:
        retries_left = 1

    while not pgtIou and retries_left:
        try:
            return PgtIOU.objects.get(tgt=pgt)
        except PgtIOU.DoesNotExist:
            if settings.CAS_PGT_FETCH_WAIT:
                time.sleep(1)
            retries_left -= 1
            logger.info('Did not fetch ticket, trying again.  {tries} tries left.'.format(
                tries=retries_left
            ))
    raise CasTicketException("Could not find pgtIou for pgt %s" % pgt)