Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to pass argument to job queue #194

Open
kz0 opened this issue Jan 25, 2018 · 3 comments
Open

Trying to pass argument to job queue #194

kz0 opened this issue Jan 25, 2018 · 3 comments

Comments

@kz0
Copy link

kz0 commented Jan 25, 2018

I'm using the following example with a simple modification:

import Queue
import time
import threading
import schedule


def job(msg):
    print(msg)


def worker_main():
    while 1:
        job_func = jobqueue.get()
        job_func()
        jobqueue.task_done()

jobqueue = Queue.Queue()

msg = 'I'm working'

schedule.every(10).seconds.do(jobqueue.put, job, msg)

worker_thread = threading.Thread(target=worker_main)
worker_thread.start()

while 1:
    schedule.run_pending()
    time.sleep(1)

But it always returns a TypeError: job() missing 1 required positional argument: 'msg', can you point me on what i'm missing?

@frankripple
Copy link

I ran your code and found the following error.

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Python\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "D:\GitHub\Study\test.py", line 14, in worker_main
    job_func()
TypeError: job() missing 1 required positional argument: 'msg'

I thought it because there is only a function in the queue. So job_func() is job() and need a parameter..
And I think this is what you want

import queue
import time
import threading
import schedule

`
def job(msg):
    print(msg)


def worker_main():
    while 1:
        (job_func,job_msg) = jobqueue.get()
        job_func(job_msg)
        jobqueue.task_done()

jobqueue = queue.Queue()

msg = 'I\'m working'

schedule.every(10).seconds.do(jobqueue.put, (job,msg))

worker_thread = threading.Thread(target=worker_main)
worker_thread.start()

while 1:
    schedule.run_pending()
    time.sleep(1)

@stenpiren
Copy link

I get the same issue when the function has multiple arguments.

@ieriii
Copy link

ieriii commented May 2, 2021

I have a related question: what if I want to pass a list as argument and schedule a job for each item in the list?

My attempt:

import queue
import time
import threading
import schedule

def job(name):
    print(name)


def worker_main():
    while 1:
        (job_func,job_msg) = jobqueue.get()
        job_func(job_msg)
        jobqueue.task_done()


name = ['jane', 'alice', 'mary']
jobqueue = queue.Queue()

schedule.every(2).seconds.do(jobqueue.put, (job, name))

worker_thread = threading.Thread(target=worker_main)
worker_thread.start()

while 1:
    schedule.run_pending()
    time.sleep(1)

Unfortunately, this approach run 1 job for all items in the list all together.
If this is not the best way to do this, feel free to point me towards an alternative solution.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants