Home > Software engineering >  I am having trouble getting multithreading working in my Python project
I am having trouble getting multithreading working in my Python project

Time:12-20

I am having trouble getting the threading to work in my code. If I take the threading out the for loop appends the menu.txt to menu = []. For my homework I need to change this so that it is working with a thread. This is what I have so far but I keep coming up with an exception 'module' object is not callable. I feel like I am close but I am missing something.

This is itemClass.py -

class item(Thread): #class for the menu items
    def __init__(self, name, wholeSale, retail, orderCount):
        Thread.__init__(self)
        self.name = name
        self.wholeSale = wholeSale
        self.retail = retail
        self.orderCount = orderCount

This is guiMain.py -

from tkinter import *
from tkinter import messagebox
from tkinter import ttk
from PIL import ImageTk,Image
import manager
import restaurant
import itemClass
import threading
from threading import *

if __name__ == "__main__":

    menu = []

    t1 = itemClass.item()
    t1.start()

    with open("menu.txt") as f: #reads menu.txt to store in menu variable
        for line in f.readlines():
            arr = line.split(",")
            menu.append(t1(arr[0],float(arr[1]),float(arr[2]),int(arr[3])))

mainGUI(menu)

This is menu.txt -

Chicken Sandwich,3.5,4.99,16
Spicy Chicken Sandwich,3.75,5.49,21
Chicken Tender Box,4.17,5.99,22
Spicy Tender Box,4.38,6.49,10
Fries,1.15,2.99,22

The Exception Being Thrown -

Message=item.__init__() missing 4 required positional arguments: 'name', 
'wholeSale', 'retail', and 'orderCount'

Source=C:\Users\neodr\Desktop\PythonClass\Lesson10_2051495\Lesson10Project2_2051495\guiMain.py StackTrace: File "C:\Users\neodr\Desktop\PythonClass\Lesson10_2051495\Lesson10Project2_2051495\guiMain.py", line 46, in (Current frame) t1 = itemClass.item()

CodePudding user response:

The error you describe is a basic one, and doesn't actually have anything to do with threading per se. The itemClass.item class you've defined expects to be given four arguments. You're not doing that when you try to create an instance of the class in your GUI code. When you do t1 = itemClass.item(), you're not passing any arguments. Later in your code you try to call t1 with arguments, but that's not supported either (an instance of your class is not callable).

None of this makes much sense. I suspect the underlying problem is that you're trying to insert threading into a place it doesn't make any sense. You should want a thread to be running where you have ongoing stuff happening and you want it to continue to happen while you're doing something else in the main thread. The part of your code that you've shown doesn't seem like it matches up with that. You're just defining menu items here. There's nothing ongoing that needs a thread.

It is likely that you need to rethink the structure of this code and put the threading somewhere else. For instance, it might make sense for a menu item, once selected, to start up a thread that keeps running in the background. But the thread creation would then be triggered by GUI stuff (selecting from the menu), not created and started up front when the menu items are being defined.

CodePudding user response:

1.you havent defined itemClass anywhere so i assume you ment item so try: from itemClass import item

 t1 = item() 

2.you havent passed any arguments to init method of item class

3.it is not clear what are you trying to achive with the thread

  • Related