1

Say I have a list of dicts containing payload data for GET requests, like the following:

lst = [{'json1': 1},{'json2': 2},{'json3': 3}]

This list can be of arbitary length in a range of 1 - 10. From here I am sending this list to a for loop of threads, that call independent functions (call_1 through to call_10):

for k in lst():
    thread.start_new_thread(call_1, ( lst[k] ,))
    thread.start_new_thread(call_2, ( lst[k] ,))
    ...
    thread.start_new_thread(call_10,( lst[k] ,))

My question is, if the list is only of length 3, say, what method should I use so that I only call the first 3 threads?

Any suggestions welcome, thanks.

ajsp
  • 2,512
  • 22
  • 34
  • Please can you show us what `k` ? – Adi219 Apr 29 '18 at 12:56
  • @Adi219 `k` is a dict `{'json1':1}` etc. – ajsp Apr 29 '18 at 12:58
  • Ohh ... I've edited my answer :) – Adi219 Apr 29 '18 at 12:59
  • 3
    Are your functions _really_ named `call_1`, `call_2`, etc? Why don't you put them into a list? – PM 2Ring Apr 29 '18 at 13:02
  • @PM 2Ring no, just trying to make the question accessible. – ajsp Apr 29 '18 at 13:08
  • @@PM 2Ring the functions need to be independent because they each access an independent TCP socket, so numbering them, or using some kind of sqeuential marker is the only way im affraid. – ajsp Apr 29 '18 at 13:13
  • 3
    You can name them anything you like. But it will be easier to loop over them if you put them into a list. – PM 2Ring Apr 29 '18 at 13:14
  • ANd even if you do need a bunch of independent functions, you can probably _create_ them in a loop using a function that returns functions. – PM 2Ring Apr 29 '18 at 13:15
  • @PM 2Ring I can't put them in a list because of the way threads are called, i.e. `(call_1, ({'json':1} ,)` – ajsp Apr 29 '18 at 13:18
  • So what? Let's say the list of functions is called `funcs`. Then you can do `(funcs[1], ({'json':1} ,)`, etc. BTW, you should take a look at https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – PM 2Ring Apr 29 '18 at 13:20
  • @ajsp How is that a problem? Just iterate over your list of functions like `for func in func_list: thread.start_new_thread(func, ( lst[k] ,))` – Aran-Fey Apr 29 '18 at 13:20
  • 2
    @PM 2Ring, @Aran-Fey you're right guys, that is the way to go. `for k, j in enumerate(qurey_lst): thread.start_new_thread(func_list[k],( j ))` – ajsp Apr 29 '18 at 13:28
  • @Aran-Fey should I delete this question then? – ajsp Apr 29 '18 at 13:31
  • 1
    Excellent. There's no need to delete the question. – PM 2Ring Apr 29 '18 at 13:31

1 Answers1

0

Try this:

counter = 0
for k in lst:
    counter += 1
    exec("thread.start_new_thread(call_" + str(counter) + ", ( lst[k] ,))" )

I'm using exec(), which treats strings as code. The string in question is being dynamically created using counter.

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • I thought about trying this but I have reservations about calling threads in exec. I get this error """unqualified exec is not allowed in function "multi_call" because it contains a nested function with free variables [line 61]""" – ajsp Apr 29 '18 at 13:10
  • This won't work because the functions aren't actually named like that. – PM 2Ring Apr 29 '18 at 13:13
  • @ajsp Honestly speaking, I don't know why that's happening ... – Adi219 Apr 29 '18 at 13:13
  • 4
    Please don't spread bad programming practices. The correct solution here is to store the functions in a list and use a loop to iterate over them. – Aran-Fey Apr 29 '18 at 13:13
  • @PM2Ring Then it's the OP's job to edit the code as they haven't told us what the functions *are* named as – Adi219 Apr 29 '18 at 13:14