Jump to content


import threading, time

def timer():
    for x in range(5):
        time.sleep(1)
        print("x is: ", x)
        

def printer():
    print("printer")
 
    
t = threading.Thread(target = timer())
t2 = threading.Thread(target = printer())

t.start()
t2.start()

 

Why is thread 2 (t2) executing only after thread 1 (t) is done?Shouldn't they run concurrently?

 

 

Link to post
Share on other sites

from multiprocessing import Process
import time

def timer():
    for x in range(5):
        time.sleep(1)
        print("x is: ", x)

def printer():
    print("printer")

if __name__ == '__main__':
    Process(target=timer).start()
    Process(target=printer).start()

 

Try this

Link to post
Share on other sites

51 minutes ago, Zeko369 said:

 

Try this

It's still working as before (not concurrently / in parallel).

 

I know that multiprocessing can run in parallel unlike threads, but I want to know why my threads aren't running concurrently.

 

EDIT:

Wait, my target has parentheses (target=printer()), it shouldn't have those and after removing them both threads run :$

Link to post
Share on other sites

8 minutes ago, MyName13 said:

It's still working as before (not concurrently / in parallel).

image.png.77b6337df0a46302f17458cc0f6a54db.png

 

Works fine for me with this code

 

from multiprocessing import Process
import time

def timer():
    for x in range(5):
        time.sleep(1)
        print("x is: ", x)

def printer():
    while(1):
        print("printer")
        time.sleep(0.5)

if __name__ == '__main__':
    Process(target=timer).start()
    Process(target=printer).start()

 

Link to post
Share on other sites

7 minutes ago, Zeko369 said:

 

Works fine for me with this code

 


from multiprocessing import Process
import time

def timer():
    for x in range(5):
        time.sleep(1)
        print("x is: ", x)

def printer():
    while(1):
        print("printer")
        time.sleep(0.5)

if __name__ == '__main__':
    Process(target=timer).start()
    Process(target=printer).start()

 

I put parentheses in the target, threading version works now, but multiprocessing doesn't (nothing happens).

 

EDIT: interesting, it doesn't work in IDLE but works in CMD :/ it also doesn't work without if name == main (in IDLE nothing is displayed, but in CMD lots of errors are printed out).

Link to post
Share on other sites

5 minutes ago, MyName13 said:

I put parentheses in the target, threading version works now, but multiprocessing doesn't (nothing happens).

 

EDIT: interesting, it doesn't work in IDLE but works in CMD :/ it also doesn't work without if name == main (in IDLE nothing is displayed, but in CMD lots of errors are printed out).

Without the name==main it will recursively call itself IIRC

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

14 minutes ago, MyName13 said:

What will call itself?Programs can work without that line too (you can simply write Main() instead of that).

Programs can work without the line which is actually what causes the problem.

When you start a new process you create a new instance of the Python interpreter which interprets the source file and then runs the function.

This means that anything that is not within name==main will be executed.

If you dont use name==main then it would also execute the line that creates a new process, creating a recursion.

 

On my install Python will recognize the problem and throw an error instead of getting into an endless recursion.

However, when using multiprocessing Pool object with map_async it does recurse and crash without the main call:

from multiprocessing import Pool

def inc(x):
	return x+1

p = Pool()
result = p.map_async(inc, [1,2,3,4,5])
print(result.get())

 

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

EDIT: ignore this post. Post notifications on iOS are buggy

Desktop: Intel i9-10850K (R9 3900X died 😢 )| MSI Z490 Tomahawk | RTX 2080 (borrowed from work) - MSI GTX 1080 | 64GB 3600MHz CL16 memory | Corsair H100i (NF-F12 fans) | Samsung 970 EVO 512GB | Intel 665p 2TB | Samsung 830 256GB| 3TB HDD | Corsair 450D | Corsair RM550x | MG279Q

Laptop: Surface Pro 7 (i5, 16GB RAM, 256GB SSD)

Console: PlayStation 4 Pro

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×