Jump to content

Python3 - running two functions at once

hirushaadi

Language: Python 3

 

What i tried:

from multiprocessing import Process
from threading import Thread
 
what i want to do:

i want to run these two functions at the same time

SHOW_THE_GUI()
ANOTHER_FUNCTION() # at the same time 

and the ANOTHER_FUNCTION() has a time.sleep(10). so, whenever its been excecated, it says that Python3 is not responding for the last 4 seconds and come back to the normal state. i dont want that to happen. i want both of the functions to run parallelly 

 

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Sauron said:

it's not enough to just import the libraries, you need to spawn new threads or processes.

https://docs.python.org/3/library/multiprocessing.html

sorry i didnt mention what i did completely!

 

def IDEK_BRUH():

    processes = []
    # num_processes = os.cpu_count()

    p = Process(target=shit)
    processes.append(p)

    p2 = Process(target=swgui.TO_DO_LIST_GUI())
    processes.append(p2)
    

    for p in processes:
        p.start()

    for p in processes:
        p.join() 

if __name__ == '__main__':
    IDEK_BRUH()

 but, still, this runs one after one

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, hirusha.adikari said:

 but, still, this runs one after one

How do you know that?

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Sauron said:

How do you know that?

because, 1 function creates some folders and files, delete some files while the other function shows a GUI to distract the user. 

 

if i put p1 as gui and p2 as other_function, the other_function wont run until i close the gui ( tkinter window ) and if i change the order, the GUI wont show up until the other_functions finishes executing.

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

22 minutes ago, hirusha.adikari said:

because, 1 function creates some folders and files, delete some files while the other function shows a GUI to distract the user. 

 

if i put p1 as gui and p2 as other_function, the other_function wont run until i close the gui ( tkinter window ) and if i change the order, the GUI wont show up until the other_functions finishes executing.

I don't see any problems with the starting method, if it doesn't work there must be something blocking your process inside the function you're calling.

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Sauron said:

I don't see any problems with the starting method, if it doesn't work there must be something blocking your process inside the function you're calling.

shall i message you the code? because i think, sharing it here will violate some Rules of this forum

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

44 minutes ago, hirusha.adikari said:

shall i message you the code? because i think, sharing it here will violate some Rules of this forum

it's best to post it here using code tags, you can use spoilers if it's too long

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

The problem may be with you calling join. That makes it join the master thread and blocks until whatever called join finishes.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Sauron said:

it's best to post it here using code tags, you can use spoilers if it's too long

its something that harms the computer and i think, sharing that kind of things will get this thread closed and even may block my account ( im not sure about that 🙂 )

 

5 hours ago, tikker said:

The problem may be with you calling join.

Ok! thanks, ill try!

hey! i know to use a computer

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, shadow_ray said:

Cpython does not support multithreading as far as I know.

That's partially right. You can spawn different threads by the same Python process but they will not run in parallel (they will alternate; while one is sleeping, another one will be running). But multiprocessing (spawning two separate Python processes by the same script) is a workaround.

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

×