python 实例方法看到实例变量_python中为什么类的实例方法可以直接访问全局变量?...

# Python queue队列,实现并发,在网站多线程推荐最后也一个例子,比这货简单,但是不够规范

# encoding: utf-8

__author__ = 'yeayee.com' # 由本站增加注释,可随意Fork、Copy

from queue import Queue # Queue在3.x中改成了queue

import random

import threading

import time

class Producer(threading.Thread):

"""

Producer thread 制作线程

"""

def __init__(self, t_name, queue): # 传入线程名、实例化队列

threading.Thread.__init__(self, name=t_name) # t_name即是threadName

self.data = queue

"""

run方法 和start方法:

它们都是从Thread继承而来的,run()方法将在线程开启后执行,

可以把相关的逻辑写到run方法中(通常把run方法称为活动[Activity]);

start()方法用于启动线程。

"""

def run(self):

for i in range(5): # 生成0-4五条队列

print("%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), i)) # 当前时间t生成编号d并加入队列

self.data.put(i) # 写入队列编号

time.sleep(random.randrange(10) / 5) # 随机休息一会

print("%s: %s producing finished!" % (time.ctime(), self.getName)) # 编号d队列完成制作

class Consumer(threading.Thread):

"""

Consumer thread 消费线程,感觉来源于COOKBOOK

"""

def __init__(self, t_name, queue):

threading.Thread.__init__(self, name=t_name)

self.data = queue

def run(self):

for i in range(5):

val = self.data.get()

print("%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val)) # 编号d队列已经被消费

time.sleep(random.randrange(10))

print("%s: %s consuming finished!" % (time.ctime(), self.getName())) # 编号d队列完成消费

def main():

"""

Main thread 主线程

"""

queue = Queue() # 队列实例化

producer = Producer('Pro.', queue) # 调用对象,并传如参数线程名、实例化队列

consumer = Consumer('Con.', queue) # 同上,在制造的同时进行消费

producer.start() # 开始制造

consumer.start() # 开始消费

"""

join()的作用是,在子线程完成运行之前,这个子线程的父线程将一直被阻塞。

join()方法的位置是在for循环外的,也就是说必须等待for循环里的两个进程都结束后,才去执行主进程。

"""

producer.join()

consumer.join()

print('All threads terminate!')

if __name__ == '__main__':

main()

"""运行结果:

Thu Feb 4 11:05:48 2016: Pro. is producing 0 to the queue!

Thu Feb 4 11:05:48 2016: Pro. is producing 1 to the queue!

Thu Feb 4 11:05:48 2016: Con. is consuming. 0 in the queue is consumed!

Thu Feb 4 11:05:49 2016: Pro. is producing 2 to the queue!

Thu Feb 4 11:05:50 2016: Pro. is producing 3 to the queue!

Thu Feb 4 11:05:51 2016: Pro. is producing 4 to the queue!

Thu Feb 4 11:05:52 2016: Con. is consuming. 1 in the queue is consumed!

Thu Feb 4 11:05:53 2016: > producing finished!

Thu Feb 4 11:06:00 2016: Con. is consuming. 2 in the queue is consumed!

Thu Feb 4 11:06:06 2016: Con. is consuming. 3 in the queue is consumed!

Thu Feb 4 11:06:06 2016: Con. is consuming. 4 in the queue is consumed!

Thu Feb 4 11:06:12 2016: Con. consuming finished!

All threads terminate!

"""

我刚刚试了一下,上面的代码中即使在def __init__里面不传入那个queue,而是直接在run方法里面调用对应的变量,确实仍然可以直接访问,那么为什么还要有global这个关键字?

这样设计是否有不合理之处?

self.xxx和xxx的区别是否就是为了区分类的内部变量和全局变量?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值