Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a thread with a function to execute code in parallel with Python

2 Answers

0 votes
import threading
import time
 
def thread_function():
    for i in range(4):
        print('thread_function')
        time.sleep(2)
    print('END - thread_function()')
 
thrd = threading.Thread(target=thread_function)
 
thrd.start()
  
for i in range(4):
   print('python')
   time.sleep(1)
  
thrd.join()

print('END')
  
 
  
'''
run:
 
thread_function
python
python
thread_function
python
python
thread_function
thread_function
END - thread_function()
END
  
'''

 



answered Feb 4, 2020 by avibootz
edited Feb 4, 2020 by avibootz
0 votes
import threading
import time
 
def thread_function(s, n):
    print(s)    
    print(n)
    for i in range(4):
        print('thread_function')
        time.sleep(2)
    print('END - thread_function(s, n)')
 
thrd = threading.Thread(target=thread_function, args=('abc', 345))
 
thrd.start()
  
for i in range(4):
   print('python')
   time.sleep(1)
  
thrd.join()

print('END')

  
 
  
'''
run:
 
abc
python
345
thread_function
python
python
thread_function
python
thread_function
thread_function
END - thread_function(s, n)
END
  
'''

 



answered Feb 4, 2020 by avibootz
edited Feb 4, 2020 by avibootz

Related questions

1 answer 155 views
1 answer 265 views
265 views asked Dec 11, 2018 by avibootz
1 answer 234 views
1 answer 131 views
131 views asked Sep 28, 2019 by avibootz
2 answers 193 views
...