threading.Thread(target=infiniteloop1('aaa'))
does not call infiniteloop1
on a separate thread.
threading.Thread
's target
argument should be a callable. Instead, infiniteloop1('aaa')
evaluates (=calls) infiniteloop1
even before threading.Thread
is called, and thread1.start
is never even reached.
You would have found this out on yourself if infiniteloop1
did not actually create an infinite loop. For example, if it was to simply return the argument it got:
def infiniteloop1(a):
return a
thread1 = threading.Thread(target=infiniteloop1('aaa'))
the error TypeError: 'str' object is not callable
would have been raised. This is because infiniteloop1('aaa')
is evaluated to 'aaa'
which is then passed to
threading.Thread
as target
.
Instead, provide a callable to threading.Thread
and the arguments for it separately with the args
kwarg (and keep in mind that it expects an iterable, so a single string argument should be passed as a one-tuple).
thread1 = threading.Thread(target=infiniteloop1, args=('aaa',))
thread1.start()
thread2 = threading.Thread(target=infiniteloop1, args=('bbb',))
thread2.start()
output:
aaa
bbb
bbbaaa
aaa
bbb
aaa
bbb
aaa
bbb
aaabbb
aaabbb
bbb
aaa
aaabbb