-1

I want to :

1/Fetch youtube-channel or playlist

2/take the url of videos.

3/process them : view them to the user, or start downloading by anotrher download manager.

Function to get information from youtube-dl :

thanks to jaimeMF answer

import time 
import youtube_dl
def myfunc(url):
    ydl = youtube_dl.YoutubeDL()
    # Add all the available extractors
    ydl.add_default_info_extractors()
    result = ydl.extract_info( url , download=False)

    ##  ydl.extract_info() collect `url fetched info` in local variable called "ie_result"; It return this "ie_result" when finshed.
    if 'entries' in result:       # Can be a playlist or a list of videos
                      video = result['entries'][0]
    else:
        # Just a video
        video = result      # get desired data
      res.append(desired data)
      return res

Problem:

If the url is youtube:channel or youtube:playlist; youtube-dl will fetch urls one by one; consuming long time to return information; you can imagine the time of fetching cannel contaiing 500 videos information.

So I want to get the res list simultaneously; to start viewing them to the user or start downloading them by another downloader. etc.

Trial:

I think in the following design :

def get_res():
     args    = ['http://www.youtube.com/channel/url']
     thread1 = threading.Thread(target=myfun , args )
     thread1.run()     
     #  access myfun.res.ie_result by anyway  #.... this is the problem
     # return its value     

def worker():
     processed_res = []
     while True:
         res = get_res()
         for item in res :
             if item not in processed_res:
                  # do something like; 
                  # start viewing it to the user, 
                  # or start downloading them by another downloader.
                  processed_res.append(item) 
         time.sleep(1)             
         # break when tread1 terminate 

get_res()
worker()

The code defect:

The actual problem remain in *access myfun.res.ie_result by anyway * in the function get_res;

in brief:

Is it possible to access the local variable of running function from other thread ? Any help is appreciated :D Any trial to solve the problem by another way is also.


* Items to be remembered :*

I have edited the code to bit clarify it.

the myfun call the extract_info() function. in its result local variable.

the extract_info() function collect results in variable called ie_result

extract_info fetch the url data one by one the return all in a list. collecting it.

The myfun.res.ie_result is equal to ydl.extract_info(url).ie_result

Community
  • 1
  • 1
esnadr
  • 427
  • 3
  • 18
  • 3
    No, you can't do that. – BrenBarn Jun 18 '14 at 04:44
  • 2
    It's not clear what you want to accomplish anyway. Do you want to obtain a partial `res` while it is still being collected by the other function? Why? – tripleee Jun 18 '14 at 04:55
  • Can you tell us exactly what you want to do, rather than telling us what code you think will do it? We are trying to guess what you mean from your code, but I don't think your code is what you mean. – daviewales Jun 18 '14 at 05:53
  • (Normally it's good to include your code, but in this case you haven't included enough extra information.) – daviewales Jun 18 '14 at 05:54
  • 1
    Note that you should call the [`start()`](https://docs.python.org/2/library/threading.html#threading.Thread.start) method of a `Thread` if you want to get any parallelism. Calling `run()` will just result in a normal function call. – Bakuriu Jun 18 '14 at 06:03
  • Thanks for your time, `Do you want to obtain a partial res while it is still being collected by the other function?` the answer is `Yes`.`WHY?` the answer is `To save user time` – esnadr Jun 19 '14 at 01:34
  • the code have been edited many times??; Is it clear now ?? – esnadr Jun 19 '14 at 03:46

1 Answers1

-1

Your question is a bit vague and code in context is required to answer it. But I think classes should be the solution You need to create a class and list your function definitions in it. You should then add the prefix 'self.' to any local variable you want to access globally.

For how to create a class: https://docs.python.org/2/tutorial/classes.html

Pearl Philip
  • 883
  • 2
  • 11
  • 16