Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

how make use of multiprocessing in python to make api calls simultaneously

$
0
0

// function to verify user login

def get_authenticated_service(file,client_secret_file):  credentials = None  if os.path.exists(file):    print('Loading Credentials From File...')    with open(file, 'rb') as token:        credentials = pickle.load(token)    return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)  if not credentials or not credentials.valid:    if credentials and credentials.expired and credentials.refresh_token:        print('Refreshing Access Token...')        credentials.refresh(Request())    else:        print('Fetching New Tokens...')        flow = InstalledAppFlow.from_client_secrets_file(            client_secret_file,            scopes=['https://www.googleapis.com/auth/youtube.readonly','https://www.googleapis.com/auth/youtube.upload','https://www.googleapis.com/auth/youtube.force-ssl'            ]        )        flow.run_local_server(port=8000, prompt='consent',                              authorization_prompt_message='')        credentials = flow.credentials        # Save the credentials for the next run        with open(file, 'wb') as f:            print('Saving Credentials for Future Use...')            pickle.dump(credentials, f)    return build(API_SERVICE_NAME, API_VERSION, credentials = credentials)

//function for verify user and make https body request

def initialize_upload( options, file, tokenFile, client_secret_file):  youtube = get_authenticated_service(tokenFile,client_secret_file)  tags = None  if options.keywords:    tags = options.keywords.split(',')  body=dict(    snippet=dict(      title=options.title,      description=options.description,      tags=tags,      categoryId=options.category    ),    status=dict(      privacyStatus=options.privacyStatus    )  )  # Call the API's videos.insert method to create and upload the video.  print("running")  insert_request = youtube.videos().insert(    part=','.join(body.keys()),    body=body,    media_body=MediaFileUpload(file, chunksize=-1, resumable=True)  )  print("multiprocessing start")  resumable_upload(insert_request)  print("multiprocessing ends")

//function for uploading file

def resumable_upload(request):  response = None  error = None  retry = 0  print("running by " + multiprocessing.current_process().name)  while response is None:    try:      print('Uploading file...')      status, response = request.next_chunk()      print('Uploading file done...')      if response is not None:        if 'id' in response:          print('Video id "%s" was successfully uploaded.' % response['id'])        else:          exit('The upload failed with an unexpected response: %s' % response)    except HttpError as e:      if e.resp.status in RETRIABLE_STATUS_CODES:        error = 'A retriable HTTP error %d occurred:\n%s' % (e.resp.status,e.content)      else:        raise    except RETRIABLE_EXCEPTIONS as e:      error = 'A retriable error occurred: %s' % e    if error is not None:      print(error)      retry += 1      if retry > MAX_RETRIES:        exit('No longer attempting to retry.')      max_sleep = 2 ** retry    #   sleep_seconds = random.random() * max_sleep      sleep_seconds = 0.5      print('Sleeping %f seconds and then retrying...' % sleep_seconds)      time.sleep(sleep_seconds)

// then title, description etc passing as arguments and making multiprocessing call

if __name__ == '__main__':  parser = argparse.ArgumentParser()  parser.add_argument('--title', help='Video title', default='Android react native code sample(ANdroid emulator)')  parser.add_argument('--description', help='Android react native code sample description',    default='Android react native code sample description(ANdroid emulator)')  parser.add_argument('--category', default='22',    help='Numeric video category. '+'See https://developers.google.com/youtube/v3/docs/videoCategories/list')  parser.add_argument('--keywords', help='react native, react, android, emulator',    default='react native, react')  parser.add_argument('--privacyStatus', choices=VALID_PRIVACY_STATUSES,    default='private', help='Video privacy status.')  args = parser.parse_args()try:    l = []    start = time.perf_counter()    t1 = multiprocessing.Process(target=initialize_upload, args=(args, "android1.mp4", 'token1.pickle','client_secret.json'))    t2 = multiprocessing.Process(target=initialize_upload, args=(args, "android1.mp4", 'token2.pickle','client_secret.json'))    t1.start()    t2.start()    t1.join()    t2.join()    finish = time.perf_counter()    print(f'Finished in {round(finish-start, 2)} seconds..')  except HttpError as e:    print('An HTTP error %d occurred:\n%s' % (e.resp.status, e.content))

if i try to make this call it takes 12.09 secs and without multiprocessing (i.e calling simultaneously one after another it takes 12.39 secs). after debugging little bit i get to know that till print("uploading file...) (in resumable_upload()) its working properly but after that its like working one at a time. Can anyone suggest how i can make him work with multiprocessing ? below is the output of this file

enter image description here


Viewing all articles
Browse latest Browse all 3831

Trending Articles