Python中的threading
2016-04-01 16:44
597 查看
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading, time
#新线程执行的代码:
def loop():
print('thread %s is running...' % threading.current_thread().name)
n = 0
while n < 5:
n = n + 1
print('thread %s ' % threading.current_thread().name)
time.sleep(3)
print('thread %s ended.' % threading.current_thread().name)
print('thread %s is running...' % threading.current_thread().name)
t = threading.Thread(target=loop, name='LoopThread')
t.start()
#t.join()
print('thread %s ended.' % threading.current_thread().name)
执行结果:
thread MainThread is running...
thread LoopThread is running...thread MainThread ended.
thread LoopThread
thread LoopThread
thread LoopThread
thread LoopThread
thread LoopThread
thread LoopThread ended.
会在主线程中创建子线程,主线程和子线程并行运行
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
import time
import threading
import random
def go(name):
# time.sleep(1)
print("go" + str(name))
time.sleep(1)
for j in range(20):
t1 = threading.Thread(group=None, target=go, name=None, args=(j,))
t1.start()
for i in range(20):
go(i)
j是并行执行,主线程和20个子线并行执行
i是串行执行,只有主线程
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
import time
import threading
import random
q = Queue.Queue()
def pro(name):
print("---pro data kaiqi")
#time.sleep(3)
for i in range(20):
time.sleep(1)
print(name + "pro data" + str(i))
q.put(i)
def con(name):
print("-----con data kaiqi")
n = 0
while n < 20:
time.sleep(3)
data = q.get()
print(name + "con data----" + str(data))
n += 1
t1 = threading.Thread(group=None, target=pro, name=None, kwargs={'name':'laoban'})
t2 = threading.Thread(group=None, target=con, name=None, kwargs={'name':'yuangong'})
t1.start()
t2.start()
两个线程并行交互,在一个队列中拿数据
# -*- coding: utf-8 -*-
import threading, time
#新线程执行的代码:
def loop():
print('thread %s is running...' % threading.current_thread().name)
n = 0
while n < 5:
n = n + 1
print('thread %s ' % threading.current_thread().name)
time.sleep(3)
print('thread %s ended.' % threading.current_thread().name)
print('thread %s is running...' % threading.current_thread().name)
t = threading.Thread(target=loop, name='LoopThread')
t.start()
#t.join()
print('thread %s ended.' % threading.current_thread().name)
执行结果:
thread MainThread is running...
thread LoopThread is running...thread MainThread ended.
thread LoopThread
thread LoopThread
thread LoopThread
thread LoopThread
thread LoopThread
thread LoopThread ended.
会在主线程中创建子线程,主线程和子线程并行运行
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
import time
import threading
import random
def go(name):
# time.sleep(1)
print("go" + str(name))
time.sleep(1)
for j in range(20):
t1 = threading.Thread(group=None, target=go, name=None, args=(j,))
t1.start()
for i in range(20):
go(i)
j是并行执行,主线程和20个子线并行执行
i是串行执行,只有主线程
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import Queue
import time
import threading
import random
q = Queue.Queue()
def pro(name):
print("---pro data kaiqi")
#time.sleep(3)
for i in range(20):
time.sleep(1)
print(name + "pro data" + str(i))
q.put(i)
def con(name):
print("-----con data kaiqi")
n = 0
while n < 20:
time.sleep(3)
data = q.get()
print(name + "con data----" + str(data))
n += 1
t1 = threading.Thread(group=None, target=pro, name=None, kwargs={'name':'laoban'})
t2 = threading.Thread(group=None, target=con, name=None, kwargs={'name':'yuangong'})
t1.start()
t2.start()
两个线程并行交互,在一个队列中拿数据
相关文章推荐
- Python中线程编程之threading模块的使用详解
- python threading模块操作多线程介绍
- python中threading超线程用法实例分析
- 举例详解Python中threading模块的几个常用方法
- Python THREADING模块中的JOIN()方法深入理解
- Python threading多线程编程实例
- python基于queue和threading实现多线程下载实例
- Python中threading模块join函数用法实例分析
- python多线程队列安全
- 线程之间的通信
- 使用队列让线程同步
- Python使用条件变量保持线程同步
- Python简单线程同步
- Python线程的daemon属性
- Python线程的线程名
- Python线程isAlive方法
- Python的Threading模块的Thread对象的join方法
- [Python module]使用threading模块实现多线程编程一[综述]
- Python 多线程编程threading模块
- [Python]多线程--threading模块实现