Raady
Raady

Reputation: 1726

Running a function on thread gives an error

I am trying to run a function "generate_model" on thread which takes three arguments.

def thread_for_generate_model(Thread):
    def __init__(self, name, job_id, boolean_string, Batch_size):
        self.name = name
        self.job_id = job_id
        self.boolean_string = boolean_string
        self.Batch_size = Batch_size

    def run(self):
        LOGGER.info("vector model create started for job_id: %s on thread %s", self.job_id, self.name)
        generate_model(self.job_id, self.boolean_string, self.Batch_size)
        LOGGER.info("vector model created for job_id: %s", self.job_id)

def main(): 
     ....
     thread_for_generate_model("Thread_for_vectormodel", job_id, generate_search_string(job_id,keywords), 5000).start()
     # I am trying to run this function on a thread
     # generate_model(job_id, generate_search_string(job_id,keywords), 5000)
     ....

I got an error ,

TypeError: thread_for_generate_model() takes 1 positional argument but 4 were given

by solution in the link, I have modified as below by adding an additional parameter

 def run(self, event=  None)

but still has the same error. how to rectify it?

Upvotes: 0

Views: 37

Answers (1)

gabhijit
gabhijit

Reputation: 3605

Code below should do what you are trying to do - I have just added a few dummy functions etc. to get the code not throw syntax error or undefined functions/variables etc.. This is roughly the structure you can follow.

As pointed out in the comments - use def something to define a method. and class Something to define a class.

from threading import Thread
import logging
import time

LOGGER = logging.getLogger()
logging.basicConfig()

class thread_for_generate_model(Thread):
    def __init__(self, name, job_id, boolean_string, Batch_size):
        Thread.__init__(self)
        self.name = name
        self.job_id = job_id
        self.boolean_string = boolean_string
        self.Batch_size = Batch_size

    def run(self):
        LOGGER.info("vector model create started for job_id: %s on thread %s", self.job_id, self.name)
        generate_model(self.job_id, self.boolean_string, self.Batch_size)
        LOGGER.info("vector model created for job_id: %s", self.job_id)

def generate_search_string(job_id, keywords):
    return False

def generate_model(job_id, string, batch_size):
    while True:
        time.sleep(1)
def main():

    job_id = 0
    keywords = ['a', 'b']
    thread_for_generate_model("Thread_for_vectormodel", job_id, generate_search_string(job_id,keywords), 5000).start()
    # I am trying to run this function on a thread
    # generate_model(job_id, generate_search_string(job_id,keywords), 5000)


if __name__ == '__main__':
        main()

Upvotes: 1

Related Questions