Created
March 14, 2014 09:41
-
-
Save ranlix/9544867 to your computer and use it in GitHub Desktop.
初尝多线程:目标成果:打印a,b,c ——sleep(1)——打印a,b,c——sleep(1)——打印a,b,c……当前的结果还是有问题的
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
# x = 0 | |
def write(text): | |
# global x | |
lock.acquire() | |
while 1: | |
print text, | |
lock.release() | |
time.sleep(1) | |
# x += 1 | |
break | |
def line(text): | |
write('%s\n' % text) | |
lock = threading.Lock() | |
if __name__ == '__main__': | |
lst = ['a', 'b', 'c'] | |
while 1: | |
for i in lst: | |
new_thread = threading.Thread(target=line, args=(i,)) | |
new_thread.start() | |
new_thread.join() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment