2

My requirement is after giving control to "thread_func", while loop should continue without wait for completion of "thread_func".

Please suggest me how to approach?

def thread_func(mySeries):
    time.sleep(30)
    print("first value is:: ", mySeries.iloc[0])
    print("high value is:: ", mySeries.max())
    print("low value is:: ", mySeries.min())
    print("last value is:: ", mySeries.iloc[-1])
    print("=" * 20)


def testfunc():
     while True:
        data = myfunc(loop_stop_flag,1)
        mySeries = Series(data)
        my_thread = Thread(target=thread_func(mySeries))
        my_thread.daemon = True  # Daemonize thread
        my_thread.start()  # Start the execution
        stop_flag = False
1
  • 1
    this my_thread = Thread(target=thread_func(mySeries)) should be my_thread = Thread(target=thread_func, args=mySeries)) Commented Dec 15, 2018 at 8:46

2 Answers 2

3

The following line:

    my_thread = Thread(target=thread_func(mySeries))

evaluates thread_func(mySeries) before invoking the constructor of Thread - this is because it tries to pass the result of thread_func as target.

The target parameter should be passed a function object, so the correct construction would look like this:

    my_thread = Thread(target=thread_func, args=(mySeries,))
Sign up to request clarification or add additional context in comments.

Comments

-2

Created a very simple class for the threading aspect. Did not duplicate mySeries, though should be simple to adapt.

from threading import Thread
from time import sleep

class thread_func (Thread):

def __init__(self):
    Thread.__init__(self)
    self.first = 'First'

def run(self):
    sleep(5)
    print("first value is:: ", self.first)
    print("high value is:: ", self.first)
    print("low value is:: ", self.first)
    print("last value is:: ", self.first)
    print("=" * 20)

if __name__ == '__main__':
    my_thread = thread_func()
    my_thread.daemon = False  # Daemonize thread
    my_thread.start()  # Start the execution
    print('Carry on')
    sleep(3)
    my_thread2 = thread_func()
    my_thread2.daemon = False  # Daemonize thread
    my_thread2.start()  # Start the execution
    print('Three seconds in')
    sleep(3)
    print('End of main')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.