2017-12-27 274 views
0

我想知道是否有人可以在使用schedule函數庫調用工作函數時如何傳遞參數。我發現在使用線程和run_threaded函數時,有一些相同的示例,但沒有任何示例。如何使用調度庫調用函數時傳遞參數?

在下面的代碼片段中,我試圖通過'sample_input'作爲參數,並混淆瞭如何定義此參數。

def run_threaded(job_func): 
job_thread = threading.Thread(target=job_func) 
job_thread.start() 

@with_logging 
def job(input_name): 
    print("I'm running on thread %s" % threading.current_thread()) 
    main(input_name) 

schedule.every(10).seconds.do(run_threaded, job(‘sample_input’)) 

回答

1

您可以通過更改方法定義並調用簽名來得到類似如下的內容。

# run_threaded method accepts arguments of job_func 
def run_threaded(job_func, *args, **kwargs): 
    print "======", args, kwargs 
    job_thread = threading.Thread(target=job_func, args=args, kwargs=kwargs) 
    job_thread.start() 

# Invoke the arguments while scheduling. 
schedule.every(10).seconds.do(run_threaded, job, "sample_input") 
相關問題