2015-08-28 25 views
1

我試圖輪詢以查看youtube-dl作業的狀態。我無法弄清楚如何使這個工作。python-rq任務中的Youtube-dl監控狀態

以下是我的python-RQ worker.py文件

class MyLogger(object): 
    def debug(self, msg): 
     pass 

    def warning(self, msg): 
     pass 

    def error(self, msg): 
     print(msg) 

def my_hook(d): 
    if d['status'] == 'finished': 
     print('Done downloading, now converting ...') 


    ydl_opts = { 
    'format': 'bestaudio/best', # choice of quality 
    'extractaudio' : True,  # only keep the audio 
    'outtmpl': temp_filepath, # name the location 
    'noplaylist' : True,  # only download single song, not playlist 
    'prefer-ffmpeg' : True, 
    'postprocessors': [{ 
     'key': 'FFmpegExtractAudio', 
     'preferredcodec': 'mp3', 
     }], 
    'logger': MyLogger(), 
    'progress_hooks': [my_hook], 
    } 

    with youtube_dl.YoutubeDL(ydl_opts) as ydl: 
     result = ydl.download([url]) 

在我主要的應用程序我都想盡變異似乎仍不能得到記錄器/ my_hook輸出。

job = q.fetch_job(job_id) 

一旦我有JOB_ID我似乎唯一能夠得到的是job.result,而如果它沒有這樣做只是返回None,我嘗試打印看狀態,但似乎無法找到它。

我可能會想讓my_hood或logger寫入一個單獨的臨時文件,我可以讀取該行,但我認爲這可能會過度? 任何幫助將不勝感激或可能的解決辦法。

回答

1

經過github的一些幫助後,我能夠使用get_current_job來更新它並將它傳遞給meta。

class MyLogger(object): 
    def debug(self, msg): 
     print(msg) 
     job = get_current_job() 
     job.meta['progress'] = msg 
     job.save()