vinothS
Posts: 6
Joined: Sat Nov 18, 2017 9:39 am

How to run Threads in Python Concurrently

Sat Nov 18, 2017 10:01 am

Dear All,

WIFI connection: Alexa-Echo Dot with Raspberry Pi 3 B model
Bluetooth connection: Raspberry Pi 3 B model with Android

WIFI connection done using one script file and Bluetooth connection also done with one script file

Now, I want to do both concurrently (Alexa Echo Dot + WIFI + Raspberry Pi + Bluetooth + Andriod)

Is there any method to run the both python files concurrently?

I have doubt in while loop, may I use thread concept and event for trigger Thread, if it possible i want document or example codes

Regards,
VinothS

User avatar
paddyg
Posts: 2017
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: How to run Threads in Python Concurrently

Sat Nov 18, 2017 6:59 pm

Well, you can but some functionality doesn't run unless it's in the main thread. To do it put the code from script one in a function and the code from script two in a different one, set each function as the target of thread and start them both, then pause in some way until you stop the program.

Or you could just execute the two scripts in different processes, which python also does very well. The main issue then would probably be communicating between the two scripts (otherwise why wouldn't you just start each one in its own terminal).

Paddy
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

bensimmo
Posts: 1930
Joined: Sun Dec 28, 2014 3:02 pm
Location: East Yorkshire

Re: How to run Threads in Python Concurrently

Sat Nov 18, 2017 7:23 pm

There is a whole section of many different ways, look for whatever suits your needs.
threading
multiprocessing
concurrent
subprocess
Etc..


https://docs.python.org/3.5/library/concurrency.html

Return to “Python”

Who is online

Users browsing this forum: No registered users and 15 guests