Bastiat
Bastiat

Reputation: 797

Error: number of arguments and self

I am trying to figure out how to use numba to best pre-compile a long running function and put it in a QThread. Unfortunately, I am running into a variety of issues (one even being numba not printing errors, just crashing the program).

I put together a small code sample to try to at least make that work, but thats still hitting issues.

main.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

import workClass

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        t = workClass.longThread()
        t.start()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

workclass.py

import numpy as np
from PyQt5.QtCore import QThread
from numba import jit
import time

class longThread(QThread):

    def __init__(self):
        super(longThread, self).__init__()
        return None

    def run(self):
        start = int(round(time.time() * 1000))
        j = np.iinfo(np.long).max
        print(self.doStuff(j))
        print(str(int(round(time.time() * 1000)) - start))

    @jit("float64(int32)")
    def doStuff(j):

        for i in range(0, 1000000):
            j = np.sqrt(j)
        return j

When I run this, I get:

TypeError: too many arguments: expected 1, got 2 at print(self.doStuff(j))

I assume this is python passing self as well as j. But if I add self into the def for doStuff, then numba whines because it cant compile self. So, whats the proper way to set up numba?

I know is this particular instance, some things could be moved around, but this is set up as close to my real project as I could get it.

Incidentally, if I just replace the signature for dostuff with nopython=True and then add self as the first argument, it at least gets past that point and actually prints an error about compiling the type, which my real project refuses to do for some unknown reason I couldn't replicate.

Upvotes: 2

Views: 994

Answers (1)

Anshul Goyal
Anshul Goyal

Reputation: 77053

You can try defining it as a staticmethod:

@staticmethod
def doStuff(j):
    # code goes here

That way, you don't have to pass in the reference to self, and the method can still remain within the class.

Upvotes: 3

Related Questions