Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing Argument Errors
#1
I have a simple(ish) program which is supposed to start a thread which will start two periodic processes.

The roughed out code I'm using to get started is this:

import threading
import time
import vlc
import sys
import schedule

exitFlag = 0

class myThread (threading.Thread):
   def __init__(self, threadID, name):
      threading.Thread.__init__(self)
      self.threadID = threadID
      self.name = name

   def run(self):
      print("Starting " + self.name)
      schedule.every(5).seconds.do(self.every5sec, self.name)
      schedule.every(10).seconds.do(self.every10sec, self.name)

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

      print("Exiting " + self.name) #not sure this will ever be called???

   def every5sec(threadName):
        print("\n%s: %s" % (threadName, time.ctime(time.time())))
        #do summit...

   def every10sec(threadName):
        print("\n%s: %s" % (threadName, time.ctime(time.time())))
        #do summit...

# Create new threads
thread1 = myThread(1, "Thread-1")
thread1.daemon=True

# Start new Threads
thread1.start()

while 1:
    time.sleep(1)
The thread starts ok but after 5 seconds, I get this error:

Output:
PS D:\Desktop\Python Test> & C:/Users/cos/AppData/Local/Programs/Python/Python39/python.exe "d:/Desktop/Python Test/MultiThreardingTiming.py" Starting Thread-1 Exception in thread Thread-1: Traceback (most recent call last): File "C:\Users\cos\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner self.run() File "d:\Desktop\Python Test\MultiThreardingTiming.py", line 21, in run schedule.run_pending() File "C:\Users\cos\AppData\Local\Programs\Python\Python39\lib\site-packages\schedule\__init__.py", line 780, in run_pending default_scheduler.run_pending() File "C:\Users\cos\AppData\Local\Programs\Python\Python39\lib\site-packages\schedule\__init__.py", line 100, in run_pending self._run_job(job) File "C:\Users\cos\AppData\Local\Programs\Python\Python39\lib\site-packages\schedule\__init__.py", line 172, in _run_job ret = job.run() File "C:\Users\cos\AppData\Local\Programs\Python\Python39\lib\site-packages\schedule\__init__.py", line 661, in run ret = self.job_func() TypeError: every5sec() takes 1 positional argument but 2 were given
My understanding of the do function, is that the first argument self.every5sec was the function and the second argument self.name was the arguments for that function however this does not appear to be the case.

What am I doing wrong?

Thanks
Reply
#2
Why are you confused? The function takes 1 (threadname) and you are passing two (self, threadname). Do you want to make the function a method of myThread or do you want to change how you bind the function?
Reply
#3
(Sep-27-2021, 08:45 PM)deanhystad Wrote: Why are you confused? The function takes 1 (threadname) and you are passing two (self, threadname). Do you want to make the function a method of myThread or do you want to change how you bind the function?
Hi,
I get that the function name is the first argument of the do function, but what I don't understand is how you pass the arguments to every5sec whilst calling with the do function...

The every5sec and every10sec functions should be part of MyThread.
Reply
#4
Make it a proper method of myThread
   def every5sec(self, threadName):
        print("\n%s: %s" % (threadName, time.ctime(time.time())))
        #do summit...
Or change how you call it.
   @staticmethod
   def every5sec(threadName):
        print("\n%s: %s" % (threadName, time.ctime(time.time())))
        #do summit...
Your choice.
Reply
#5
every5sec & every10sec should have self as the first parameter
 
class myThread (threading.Thread):
   ...
 
   def every5sec(self, threadName):
        ...
 
   def every10sec(self, threadName):
        ...
Reply
#6
Many thanks for your help guys Big Grin Big Grin
Reply
#7
schedule that you use has own advice how to run Thread with it in doc.
Also try not use the old %s string formatting,use f-stringšŸ’„
So to write a example with your code.
import schedule
import time
import threading

def every5sec():
      print(f'{time.ctime()} {every5sec.__name__}')

def every10sec():
    print(f'{time.ctime()} {every10sec.__name__}')

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

if __name__ == '__main__':
    schedule.every(5).seconds.do(run_threaded, every5sec)
    schedule.every(10).seconds.do(run_threaded, every10sec)
    while True:
        schedule.run_pending()
        time.sleep(1)
Output:
G:\div_code\answer Ī» python schedule_10sek.py Wed Sep 29 08:10:50 2021 every5sec Wed Sep 29 08:10:55 2021 every10sec Wed Sep 29 08:10:55 2021 every5sec Wed Sep 29 08:11:00 2021 every5sec Wed Sep 29 08:11:05 2021 every10sec Wed Sep 29 08:11:05 2021 every5sec Wed Sep 29 08:11:10 2021 every5sec Wed Sep 29 08:11:15 2021 every10sec Wed Sep 29 08:11:15 2021 every5sec Wed Sep 29 08:11:20 2021 every5sec Wed Sep 29 08:11:25 2021 every10sec
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Passing argument from top-level function to embedded function JaneTan 2 2,273 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  SyntaxError: positional argument follows keyword argument syd_jat 3 5,847 Mar-03-2020, 08:34 AM
Last Post: buran
  Passing an argument by reference Exsul 12 4,721 Aug-22-2019, 07:29 AM
Last Post: DeaD_EyE
  passing an argument to avoid a global Skaperen 9 3,873 Jul-12-2019, 11:07 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020