I think it is a threading solution problem but I don't know how to programming it

Hello, I was running a for loop which contains two part. I want the second part of the loop can continue to run because it receive the data from first part, and I can run the first part of the next loop. So I represent number as loop number, and A B two part.

1A->(1B, 2A)->(2B, 3A)->…->((n-1)B, nA)–>nB

Can threading module achieve my concept? Thanks.

Probably.

The simplest approach probably looks like this: write your loop body to
do the first part of the loop, and put the logic for the second part in
a function. Now your loop looks like:

from threading import Thread

def second(x):
    do the second part here using x (and whatever else you need)

for x in something:
    # first part
    ... do the first part with x
    T = Thread(target=second, args=(x,))
    T.start()

That will dispatch a call to second(x) after the first part, and then
return to the top of the loop.

This has various potential issues, depending on what you want to do:

  • races: if the for-loop and second() access common dat a structures you
    need to ensurre that they do this in a thread-safe manner, for example
    by taking locks around the code working on them
  • limit the number of threads - maybe only 1 instance of second() should
    run at a time, or maybe there is a performance limit on the number of
    instances of second() which should run

But it would be a start.

Depending on what you need/want to do you might need a different
approach.

Cheers,
Cameron Simpson cs@cskk.id.au

I’m sorry, I don’t understand why you want threading here.

I don’t fully understand your loop logic, but I think all you want is
to interleave two sequences A and B, like this:

A = [1, 2, 3, 4, 5, 6]
B = ['a', 'b', 'c', 'd', 'e', 'f']

# process 1, 'a', 2, 'b', 3, 'c', etc in that order.

Is that what you want? You don’t need threads for that. Threads will
just add a HUGE amount of complexity and non-determinism (you can’t
control the order that the threads run) and hurt performance a lot.

If you want something different, please explain in more detail what you
want, but here is how you can interleave two lists like A and B.

# stop on the shortest list
def interleave(a, b):
    for x, y in zip(a, b):
        yield x
        yield y


# A shorter version of the above:
from itertools import chain
def interleave(a, b):
    return chain.from_iterator(zip(a, b))


# Now process the items.
for item in interleave(alist, blist):
    process(item)

If you want to pad the shorter list with a pad value, so that they are
the same length, change the call to “zip” to “zip_longest” like this:

from itertools import zip_longest
def interleave(a, b):
    for x, y in zip_longest(a, b, fillvalue='padding'):
        yield x
        yield y

Why I want to do this is because I define a function and in this function I can separate it into two part and the second part takes time over my expectation so I want to try when executing second part I can go to next loop and run the same function at the same time. Hope I clarify my need clear enough.

No, sorry, I still don’t understand. Can you give a specific example?

If you are expecting to process items in a specific order:

1, 2, 3, 4, 5, ...

then you can’t use one thread to do the odd numbers and another thread

to do the even numbers:

# thread A

1, 3, 5, 7, ...

# thread B

2, 4, 6, 8, ...

because you cannot control the order that A and B will run. So you might

see:

1, 2, 4, 3, 6, 5, 7, 9, 8, ...

If you don’t care about the order that the calculation is done, then

threading will be fine.

But threading in Python is very unlikely to speed up your code,

unless it involves file I/O (input/output). If the threads are reading

or writing data from a network, or disk, then you may get a performance

boost. But if they are just doing pure calculation with no I/O, it will

probably be slower.

Without knowing more about the task you are doing, it is hard to say

whether threading will be good or bad.