Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sending data to thread
#2
The easiest way is to define a function that will be the core of your thread, then create threads that will call this function and pass it different arguments (names in example below, graph number for you):

import threading,time,random

RUNS=4
THREADNAMES=['ONE','TWO','THREE']

def threadFunction(id):
    print "Starting thread %s" % id
    for _ in range(RUNS):
        time.sleep(random.uniform(.5,2.5))
        print 'Thread %s still alive' % id
    print "Ending thread %s" % id


# create the threads
threads=[threading.Thread(target=threadFunction,args=(id,)) for id in THREADNAMES]
         
# make them run         
for t in threads:
    t.start()
    
# make sure they are all ended before existing:
for t in threads:
    t.join()
However, due to the Global Interpreter Lock there is no real concurrent execution of threads in Python, so you won't run on more core with Python threading.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
sending data to thread - by kiyoshi7 - Apr-17-2017, 05:22 PM
RE: sending data to thread - by Ofnuts - Apr-22-2017, 05:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sending data from the form to DB quisatz 5 1,428 Sep-27-2023, 04:58 PM
Last Post: quisatz
  Help Sending Socket Data snippyro 0 1,082 Sep-23-2021, 01:52 AM
Last Post: snippyro
  get data (temperature and humidity) from DHT22 into CSV and sending them over the net apollo 0 3,906 Apr-16-2021, 07:49 PM
Last Post: apollo
  Error SQLite objects created in a thread can only be used in that same thread. binhduonggttn 3 15,723 Jan-31-2020, 11:08 AM
Last Post: DeaD_EyE
  sending data to second python script Cyberfly 1 3,188 Jan-29-2018, 10:09 AM
Last Post: Cyberfly
  sending data from my raspberry pi to my website mohitsangavikar 2 17,890 Sep-05-2017, 06:55 PM
Last Post: wrybread

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020