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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 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 198 views
1 answer 304 views
304 views asked Dec 11, 2018 by avibootz
1 answer 286 views
1 answer 168 views
168 views asked Sep 28, 2019 by avibootz
2 answers 241 views
...