TypeError: __init__() takes 1 positional argument but 2 were given Python

2.4k Views Asked by At
from locust import HttpLocust, TaskSet, task, between, events, Locust, User, HttpUser
import vertica_python
import time, logging

def get_sample_query():
    query = '''
        SELECT COUNT(*) FROM tst.test_table'''

    conn = {
        'host': os.environ['vertica_host'],
        'port': os.environ['vertica_port'],
        'database': os.environ['vertica_database'],
        'user': os.environ['vertica_user'],
        'password': os.environ['vertica_password'],
        'read_timeout': 600,
        'unicode_error': 'strict',
        'ssl': False
    }
    return conn, query
    
def execute_query(conn_info, query):
    with vertica_python.connect(**conn_info) as conn:
        cur = conn.cursor()
        cur.execute(query)
        return [x for x in cur.iterate()]


class VerticaClient:
    def __getattr__(self, name):
        def wrapper(*args, **kwargs):
            start_time = time.time()
            try:
                res = execute_query(*args, **kwargs)
                self._locust_environment.events.request_success.fire(request_type="vertica",
                                            name=name,
                                            response_time=int((time.time() - start_time) * 1000),
                                            response_length=len(res))
            except Exception as e:
                 self._locust_environment.events.request_failure.fire(request_type="vertica",
                                            name=name,
                                            response_time=int((time.time() - start_time) * 1000),
                                            exception=e)

            logging.info('error {}'.format(e))
        return wrapper


class VerticaTaskSet(TaskSet):
    @task
    def execute_query(self):
        self.client.execute_query(get_sample_query()[0], get_sample_query()[1])


class VerticaLocust(HttpUser):
    tasks = [VerticaTaskSet]
    wait_time = between(0.1, 1)

    def __init__(self):
        super(VerticaLocust, self).__init__()
        self.client = VerticaClient()

Hi there, I'm trying to stress test a Vertica database using the code above however, when I run the python file, I get the following error: 'TypeError: init() takes 1 positional argument but 2 were given'. I've done some research but haven't been able to determine a fix, can anyone advise/help? Thanks!

[2020-10-08 14:53:29,338] LAPTOP/WARNING/locust.main: System open file limit setting is not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.
[2020-10-08 14:53:29,339] LAPTOP/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2020-10-08 14:53:29,358] LAPTOP/INFO/locust.main: Starting Locust 1.2.3
[2020-10-08 14:53:31,799] LAPTOP/INFO/locust.runners: Spawning 2 users at the rate 2 users/s (0 users already running)...
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 417, in <lambda>
    lambda: super(LocalRunner, self).start(user_count, spawn_rate, wait=wait)
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 299, in start
    self.spawn_users(user_count, spawn_rate=spawn_rate, wait=wait)
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 194, in spawn_users
    spawn()
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 187, in spawn
    new_user = user_class(self.environment)
TypeError: __init__() takes 1 positional argument but 2 were given
2020-10-08T13:53:31Z <Greenlet at 0x203ee5c68c0: <lambda>> failed with TypeError

[2020-10-08 14:53:31,820] LAPTOP/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x203ee5c68c0: <lambda>>
Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 417, in <lambda>
    lambda: super(LocalRunner, self).start(user_count, spawn_rate, wait=wait)
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 299, in start
    self.spawn_users(user_count, spawn_rate=spawn_rate, wait=wait)
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 194, in spawn_users
    spawn()
  File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 187, in spawn
    new_user = user_class(self.environment)
TypeError: __init__() takes 1 positional argument but 2 were given
2

There are 2 best solutions below

2
On

I get the following error: 'TypeError: init() takes 1 positional argument but 2 were given'.

locust.HttpUser extends locust.User which takes an environment parameter. Whatever it is which expects to be initialising a locust User object likely passes said environment parameter, which blows up because your VerticaLocust.__init__ doesn't take any.

That aside, Python errors have a traceback, tracebacks are generally useful for post-mortem analysis.

0
On

Actually you want to create your own locust class so you need to inherit from User in previous version Locust. So you need to change you class like this:

class VerticaLocus(User):
    wait_time = between(0.1, 1)
    tasks = [VerticaTask]

    def __init__(self, environment):
        super().__init__(environment)
        self.client = VerticaClient()

Why, because I believe we are following the same tutorial.