Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python multithreading tutorials

# Python program to illustrate the concept
# of threading
import threading
import os
  
def task1():
    print("Task 1 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 1: {}".format(os.getpid()))
  
def task2():
    print("Task 2 assigned to thread: {}".format(threading.current_thread().name))
    print("ID of process running task 2: {}".format(os.getpid()))
  
if __name__ == "__main__":
  
    # print ID of current process
    print("ID of process running main program: {}".format(os.getpid()))
  
    # print name of main thread
    print("Main thread name: {}".format(threading.current_thread().name))
  
    # creating threads
    t1 = threading.Thread(target=task1, name='t1')
    t2 = threading.Thread(target=task2, name='t2')  
  
    # starting threads
    t1.start()
    t2.start()
  
    # wait until all threads finish
    t1.join()
    t2.join()
Comment

Python multithreading

import threading 
  
def print_hello_three_times():
  for i in range(3):
    print("Hello")
  
def print_hi_three_times(): 
    for i in range(3): 
      print("Hi") 

t1 = threading.Thread(target=print_hello_three_times)  
t2 = threading.Thread(target=print_hi_three_times)  

t1.start()
t2.start()
Comment

python 2.7 multithreading

from multiprocessing.pool import ThreadPool as Pool

pool_size = 10
pool = Pool(pool_size)

results = []

for region, directory_ids in direct_dict.iteritems():
    for dir in directory_ids:
        result = pool.apply_async(describe_with_directory_workspaces,
                                  (region, dir, username))
        results.append(result)

for result in results:
    code, content = result.get()
    if code == 0:
        # ...
Comment

multithreaded programming in python

import threading
 import time

 def useless_function(seconds):
     print(f'Waiting for {seconds} second(s)', end = "
")
     time.sleep(seconds)
     print(f'Done Waiting {seconds}  second(s)')

 start = time.perf_counter()
 t = threading.Thread(target=useless_function, args=[1])
 t.start()
 print(f'Active Threads: {threading.active_count()}')
 t.join()
 end = time.perf_counter()
 print(f'Finished in {round(end-start, 2)} second(s)')
Comment

PREVIOUS NEXT
Code Example
Python :: python min value index from an array 
Python :: python list contains string 
Python :: liste compréhension python 
Python :: xarray get number of lat lon 
Python :: turtle graphics documentation 
Python :: python use variable name as variable 
Python :: pandas to python datetime 
Python :: get index of all element in list python 
Python :: a int and float. python 
Python :: can serializer returns an object in django 
Python :: to_frame python 
Python :: python curl 
Python :: python get total gpu memory 
Python :: map a list to another list python 
Python :: Display an image over another image at a particular co-ordinates in openCV 
Python :: how can I corect word spelling by use of nltk? 
Python :: # enumerate 
Python :: fast input python 
Python :: how to convert response to beautifulsoup object 
Python :: pandas series example 
Python :: python boolean operators 
Python :: import module python same directory 
Python :: obtain items in directory and subdirectories 
Python :: select random img in python using os.listdir 
Python :: python not equal to symbol 
Python :: quantile calcultion using pandas 
Python :: removing duplicates from django models data 
Python :: group by pandas 
Python :: Remove whitespace from str 
Python :: tensorflow evaluation metrics 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =