Abstract: the module known as threading used for multiple threads
Superior to the module ‘thread’, the module ‘threading’ act as Java style and work better. However, GIL (Global Interpreter Lock) have effects on the implementation of multi-threads. The below script involve an run with the endless loop. Thus, there is always one thread used in the computer no matter how many threads we run.
The script:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 10:34:19 2015
@author: yuan
"""
import threading
import time
#
def loop(tid,x):
print 'ID:%s=%s\n' % (tid,x)
time.sleep(10)
#endless loop
def eloop(tid,x):
print 'ID:%s, input:%s\n' % (tid,x)
time.sleep(1)
while True:
x=x^1
#multi-threading
def multithreading():
for i in range(1,5):
print 'in:', i
t=threading.Thread(target=eloop, args=(i,i), name=i)
t.start()
t.join()
if __name__=="__main__":
multithreading()
print 'ok'
No comments:
Post a Comment