Jump to content

Python3 Help, Why doesn't this print correctly?

Poet129
Go to solution Solved by Poet129,
8 hours ago, Poet129 said:

@Eigenvektor

After some troubleshooting I found that python can't run a currently running function (aka cal in my program).

If anyone has a way of doing this let me know.

Using subprocess.Popen I was able to get around this. Thanks for the help everyone.

 

I got 0, 1, 2, 3 but I was expecting 3, 2, 1, 0. How do I fix this?

PrintFastest.png

 

Link to comment
Share on other sites

Link to post
Share on other sites

Can you please post the code as text, in a code block (see the <> symbol at the top), instead of the file?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, minibois said:

Can you please post the code as text, in a code block (see the <> symbol at the top), instead of the file?

from os import cpu_count
from time import sleep
from threading import Thread
Threads = cpu_count()
Threadsnm = 1
def cal(Threadsnm):
    sleep(Threads - Threadsnm)
    print(Threadsnm - 1)
while (Threadsnm <= Threads):
    Thread(cal(Threadsnm)).start()
    Threadsnm += 1

Edit: Edited code for recommendation below.

Link to comment
Share on other sites

Link to post
Share on other sites

You're working with threads, so stuff happens in parallel. If you want to guarantee a specific order, you typically need to use some kind of synchronization primitives and e.g. queues. This goes double if you access a shared resource from different threads.

 

Not sure about python, but stuff like "sleep" isn't necessarily very precise in most languages. It usually means "sleep at least this long". If another thread is running while you're sleeping, it may take more time before your thread is woken up and can resume work.

 

~edit: If I interpret your code correctly, your first thread should sleep for 4-0 ms, then print "0", the second one should sleep for 4-1ms then print "1" and so on.

 

You need to keep in mind that starting a thread also takes time. So those 4ms may be over before the second thread ever has the chance to run. And e.g. if you have a shared thread pool then multiple threads may not necessarily run concurrently at all.

 

Spoiler

 

I would also recommend not to use a while loop like this:



while (Threadsnm != Threads)

 

As a rule of thumb, you want to use this:



while (Threadsnm < Threads)

Not only does it better express what you're trying to do, it also protects you in case your variable never actually becomes == threads (while it's not going to happen here, just in general it is better to be safe than sorry)

 

 

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

11 minutes ago, Eigenvektor said:

~edit: If I interpret your code correctly, your first thread should sleep for 4-0 ms, then print "0", the second one should sleep for 4-1ms then print "1" and so on.

 

You need to keep in mind that starting a thread also takes time. So those 4ms may be over before the second thread ever has the chance to run. And e.g. if you have a shared thread pool then multiple threads may not necessarily run concurrently at all.

 

I thought it was is seconds not milliseconds...

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Poet129 said:

I thought it was is seconds not milliseconds...

Yeah, I guess you're right. Sorry, I'm used to Java/C#/C++ etc. where stuff like this is typically in milliseconds.

 

This makes me suspect the threads don't actually run concurrently. If I change your code slightly:

from os import cpu_count
from time import sleep
from threading import Thread
Threads = cpu_count()
Threadsnm = 1

def cal(Threadsnm):
    print("Started", Threadsnm)
    sleep(Threads - Threadsnm)
    print("Ended", Threadsnm)

while (Threadsnm <= Threads):
    Thread(cal(Threadsnm)).start()
    Threadsnm += 1

I can see that e.g. Thread 2 isn't started until Thread 1 completes.

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Eigenvektor said:

Yeah, I guess you're right. Sorry, I'm used to Java/C#/C++ etc. where stuff like this is typically in milliseconds.

 

This makes me suspect the threads don't actually run concurrently. If I change your code slightly:


from os import cpu_count
from time import sleep
from threading import Thread
Threads = cpu_count()
Threadsnm = 1

def cal(Threadsnm):
    print("Started", Threadsnm)
    sleep(Threads - Threadsnm)
    print("Ended", Threadsnm)

while (Threadsnm <= Threads):
    Thread(cal(Threadsnm)).start()
    Threadsnm += 1

I can see that e.g. Thread 2 isn't started until Thread 1 completes.

This is what I thought, I was asking for help to start them as close to concurrently as possible without having to write them all out.

Link to comment
Share on other sites

Link to post
Share on other sites

14 minutes ago, Poet129 said:

This is what I thought, I was asking for help to start them as close to concurrently as possible without having to write them all out.

Not a Python expert, but this reads like this isn't possible with such a simple loop:

https://stackoverflow.com/a/1697670

 

If I understand the answer correctly, threads don't actually run in the background until they've being going for a while and/or run I/O intensive stuff.

 

~edit: Here's more info: https://wiki.python.org/moin/GlobalInterpreterLock

Remember to either quote or @mention others, so they are notified of your reply

Link to comment
Share on other sites

Link to post
Share on other sites

15 minutes ago, Eigenvektor said:

Not a Python expert, but this reads like this isn't possible with such a simple loop:

https://stackoverflow.com/a/1697670

 

If I understand the answer correctly, threads don't actually run in the background until they've being going for a while and/or run I/O intensive stuff.

 

~edit: Here's more info: https://wiki.python.org/moin/GlobalInterpreterLock

Found this however it doesn't work either...

from os import cpu_count
from time import sleep
from multiprocessing import Process as Thread # <---- This is what I mean.
Threads = cpu_count()
Threadsnm = 1
def cal(Threadsnm):
    sleep(Threads - Threadsnm)
    print(Threadsnm - 1)
while (Threadsnm <= Threads):
    Thread(cal(Threadsnm)).start()
    Threadsnm += 1

 

Link to comment
Share on other sites

Link to post
Share on other sites

@Eigenvektor

After some troubleshooting I found that python can't run a currently running function (aka cal in my program).

If anyone has a way of doing this let me know.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Poet129 said:

@Eigenvektor

After some troubleshooting I found that python can't run a currently running function (aka cal in my program).

If anyone has a way of doing this let me know.

Using subprocess.Popen I was able to get around this. Thanks for the help everyone.

Link to comment
Share on other sites

Link to post
Share on other sites

Here's some super basic example of GoLang  goroutines  : https://play.golang.org/p/rV1yYqqJNEk

 

Hit run to run the code directly in the browser.

 

You will see that the 4 goroutines are started almost at same time, but start order is not guaranteed unless you use some techniques (semaphores, queues, channels etc)

Link to comment
Share on other sites

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

×