1

as part of a trading application which i am developing, i need to send a parameter to a Thread. I have already referred the below links and none of these are working.

Python Threading String Arguments,
How to pass arguments to thread functions in Python,
python threading - best way to pass arguments to threads

My code

order_trigger_loop_initiator = threading.Thread(target=trigger(), args=[company_data['instrument_token']])
renko_loop_initiator.start()

Part of the function which i am initiating as a thread

def RENKO_TRIMA(token):
    global ohlc_final_1min, RENKO_Final, final_position, order_quantity, RENKO, RENKO_temp, Direction, Orderid, Target_order, Target_order_id, renko_thread_running, day_profit_percent
    try:
        renko_thread_running = "YES"
        attained_profit()
        quantity()
        positions(token)

I have followed the suggestion in the above mentioned website and even tried to do stuff like

renko_loop_initiator = threading.Thread(target=RENKO_TRIMA, args=company_data['instrument_token'])

and

renko_loop_initiator = threading.Thread(target=RENKO_TRIMA, args=[company_data['instrument_token']]))
renko_loop_initiator = threading.Thread(target=RENKO_TRIMA, args=(company_data['instrument_token']))
renko_loop_initiator = threading.Thread(target=RENKO_TRIMA, args=(company_data['instrument_token'],))

Nothing seems to be working. the value that be sent as an argument would be 1270529

I get the below error message when i am trying any of the above methods.

Traceback (most recent call last):
  File "C:/Users/win10/PycharmProjects/Trading-Application/USD-INR.py", line 838, in on_ticks
    order_trigger_loop_initiator = threading.Thread(target=trigger(), args=[company_data['instrument_token']])
TypeError: trigger() missing 1 required positional argument: 'token'
K G
  • 27
  • 8
  • 1
    trigger is not supposed to be called `threading.Thread(target=trigger, args=[company_data['instrument_token']])` remove `()` after trigger – jaswanth Jul 23 '20 at 11:40

2 Answers2

1

In the error message, you are calling trigger instead of passing it as a function.

order_trigger_loop_initiator = threading.Thread(target=trigger(), ...
                                                   # uh oh ---^

Try passing just the function:

order_trigger_loop_initiator = threading.Thread(
    target=trigger, 
    args=[company_data['instrument_token']]
)
James
  • 32,991
  • 4
  • 47
  • 70
1

You're calling trigger (which you haven't posted in your question, making this guesswork) immediately there:

order_trigger_loop_initiator = threading.Thread(
  target=trigger(),
  args=[company_data['instrument_token']],
)

That might work if trigger was a function that accepted no arguments and returned another function, but if you mean to run the trigger function in another thread, you'll want to remove those parentheses:

order_trigger_loop_initiator = threading.Thread(
  target=trigger,
  args=[company_data['instrument_token']],
)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • i have actually put a wrong code, updated the function name which i was calling. Updated it. – K G Jul 23 '20 at 13:11
  • it worked. How a small mistake made a whole lot of difference. With parentheses, i was calling trigger as a function which indeed will run other functions. But without parentheses, i will run the trigger independently as a different thread – K G Jul 23 '20 at 13:48