dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.73k stars 959 forks source link

How to pass a value when using threading? #507

Open bbartling opened 2 years ago

bbartling commented 2 years ago

If I have some functions very similar to the documentation:

def job(faces):
    print("I'm running on thread %s" % threading.current_thread())
    pload = {'face_count':num_faces}
    r = requests.post('https://httpbin.org/post',data=pload)
    print(r.text)

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

How do I pass a value to the run_threaded function? Looking to pass an int value called faces

import threading
import time
import schedule
import requests

def job(faces):
    print("I'm running on thread %s" % threading.current_thread())
    pload = {'face_count':num_faces}
    r = requests.post('https://httpbin.org/post',data=pload)
    print(r.text)

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

num_faces = 0
schedule.every(30).seconds.do(run_threaded, num_faces)

try:
    while True:  
        schedule.run_pending()

This will error out, any tips appreciated:

Traceback (most recent call last):
  File "C:\Users\bbartling\OneDrive - Slipstream\Desktop\LBS\openCvTesting\test.py", line 37, in <module>
    schedule.run_pending()
  File "C:\Python39\lib\site-packages\schedule\__init__.py", line 780, in run_pending
    default_scheduler.run_pending()
  File "C:\Python39\lib\site-packages\schedule\__init__.py", line 100, in run_pending
    self._run_job(job)
  File "C:\Python39\lib\site-packages\schedule\__init__.py", line 172, in _run_job
    ret = job.run()
  File "C:\Python39\lib\site-packages\schedule\__init__.py", line 661, in run
    ret = self.job_func()
TypeError: run_threaded() missing 1 required positional argument: 'faces'
Charlez91 commented 1 year ago

Run it like dis

schedule.every(30).seconds.do(run_threaded, faces=num_faces)