Hello, I have a problem with the execution of my code, and I prolazil the entire Internet and could not find the answer to my question(sorry) and so, an elementary example:
def first() :
    sleep(30)
    print(2)
def second():
    sleep(20)
    print(1)
    
def main():
    first()
    second()
    print("Continue")
main()

I want the conclusion to be like this:

Continuing
1
2

I know I need to use AsyncIO
but I haven't been able to understand this module for quite a long time.

1 Answers 1

Streams are better suited for your task.
from time import sleep
from threading import thread


def first():
    sleep(3)
    print(2)


def second():
    sleep(3)
    print(1)
    

def main():
    Thread(target=first) .start()
    Thread(target=second) .start()
    print("Continue")


main()


PS D: \ Dropbox \ Develop \ all_tests>python threads.py
Continuing
1
2
  • The main thing is not to tell the person about the common memory and competitive appeal to shared variables, otherwise it will look much more difficult :) – Unremarkable Math Jul 22 '19 at 14:18
  • Unremarkable Math, and what is it for? – Leisure Suit Jul 22 '19 at 15:20
  • Leisure Suit, to the fact that the proposed solution implies a whole mountain of pitfalls, about which you did not warn the author(obviously a beginner).

    After all, you do not think that he really needs to call sleep and print in these functions? :)
    – Unremarkable Math Jul 22 '19 at 16:11
  • Unremarkable Math, and what are the pitfalls with them? Even I have been actively using flows in some of my works for half a year already and have not yet stumbled upon them. – Leisure Suit Jul 22 '19 at 16:16
  • Leisure Suit, you surprise me.Have you ever tried to increment a variable in one thread and check its value in another thread?

    import time
    import threading
    
    
    x=0
    
    def incr():
        global x
        if x!=10:
            time.sleep(0.1)
            x +=10
    
    t1=threading.Thread(target=incr)
    t2=threading.Thread(target=incr)
    
    t1.start()
    t2.start()
    
    t1.join()
    t2.join()
    
    print(x)
    – Unremarkable Math Jul 22 '19 at 16:24
  • Returning to the author's question: the solution proposed is correct, but it is not at all as simple as it seems at first glance.

    Before writing code that works with threads, you need to understand the essence of multithreading and thoughtfully read a couple of articles on this topic, otherwise you will suffer at every step and most likely get an unstable program that does not do what you want.
    – Unremarkable Math Jul 22 '19 at 16:44
  • Unremarkable Math, this is not specific to threads, this applies generally to concurrency.In this case, threads are the easiest way to implement parallel code execution.
    The author obviously just began to study programming and he had a question"how to execute code in parallel" - he does not need to search through all the features of multithreading, he received an answer to his question and will continue to study programming, stumbling along the way and rising.
    – Leisure Suit Jul 22 '19 at 16:55
  • Leisure Suit, in general, yes, but when using cooperative multitasking(asyncio), this problem does not arise, since the real flow of execution is only one. – Unremarkable Math Jul 22 '19 at 17:12