Python论坛  - 讨论区

标题:Re: Re: [python-chinese]请教一个关于线程的问题

2004年08月09日 星期一 20:36

杨元杰 feynman1983 at 163.com
Mon Aug 9 20:36:59 HKT 2004

limodou.你好
  为什么connection不会改变呢? thread.start_new(handleClient, (connection,))中传递不是connection的引用嘛?connection不属于线程的Shared global memory 吗?
  ps:再请教一下一个process启动了若干线程,那么它的Shared global memory 是什么?是不是这个process的中定义的变量,生成的实例?




> 杨元杰,您好!
> 
> 	每次connection都是一个新值。而线程A中的connection你并没有去改变,因此它永远使用的是旧的值。
> 
> ======= 2004-08-09 16:00:54 您在来信中写道:=======
> 
> >大家好!
> >这是《programming python》10。4中的一个例子。这例子运行在服务器端,响应客户端的请求
> >Example 10-7. PP2E\Internet\Sockets\thread-server.py 
> >#########################################################
> ># Server side: open a socket on a port, listen for
> ># a message from a client, and send an echo reply; 
> ># echos lines until eof when client closes socket;
> ># spawns a thread to handle each client connection;
> ># threads share global memory space with main thread;
> ># this is more portable than fork--not yet on Windows;
> >#########################################################
> >
> >import thread, time
> >from socket import *                     # get socket constructor and constants
> >myHost = ''                              # server machine, '' means local host
> >myPort = 50007                           # listen on a non-reserved port number
> >
> >sockobj = socket(AF_INET, SOCK_STREAM)           # make a TCP socket object
> >sockobj.bind((myHost, myPort))                   # bind it to server port number
> >sockobj.listen(5)                                # allow up to 5 pending connects
> >
> >def now(): 
> >    return time.ctime(time.time())               # current time on the server
> >
> >def handleClient(connection):                    # in spawned thread: reply
> >    time.sleep(5)                                # simulate a blocking activity
> >    while 1:                                     # read, write a client socket
> >        data = connection.recv(1024)
> >        if not data: break
> >        connection.send('Echo=>%s at %s' % (data, now()))
> >    connection.close() 
> >
> >def dispatcher():                                # listen until process killd
> >    while 1:                                     # wait for next connection,
> >        connection, address = sockobj.accept()   # pass to thread for service
> >        print 'Server connected by', address,
> >        print 'at', now() 
> >        thread.start_new(handleClient, (connection,))
> >
> >dispatcher()
> >
> >我不明白的是:
> >    在函数dispatcher()中,当服务器接到一个请求,启动了一个线程A,在线程A运行过程中,服务器又街道另个client的请求,执行connection, address = sockobj.accept(),那么线程A中的connecion不是就会被覆盖了吗?
> >
> >
> >
> >
> >
> >-------------------------------------------------------------------------------------
> >15寸液晶双子星,震天雷II--D451¥2466/D353¥2399 http://design.163.com/aocmonitor/index.html
> >_______________________________________________
> >python-chinese list
> >python-chinese at lists.python.cn
> >http://python.cn/mailman/listinfo/python-chinese
> >
> 
> = = = = = = = = = = = = = = = = = = = =
> 			
> 
>         致
> 礼!
>  
> 				 
>         limodou
>         chatme at 263.net
>           2004-08-09
> 
> 

-------------------------------------------------------------------------------------
15寸液晶双子星,震天雷II--D451¥2466/D353¥2399 http://design.163.com/aocmonitor/index.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月09日 星期一 20:57

limodou chatme at 263.net
Mon Aug 9 20:57:43 HKT 2004

mike gaul,您好!

	Python中不含有绝对的私有变量,象使用下划线的确Python会有一些特殊的处理,但你可以不去管它。不需要刻意地去使用私有变量,如果使用尽可能按约定来做。详情在python语法规范中有说明。

======= 2004-08-09 17:11:29 您在来信中写道:=======

>在手册中有这样一段:
>9.6 私有变量
>
>Python对类的私有成员提供了有限的支持。任何形如__spam 
>(以至少双下划线开头,至多单下划线结尾)随即都被替代为
>
>_classname__spam,去掉前导下划线的classname 即当前的类
>名。这种混淆不关心标识符的语法位置,所以可用来定义私有类
>实例和类变量、方法,以及全局变量,甚至于将其它类的实例保
>存为私有变量。混淆名长度超过255个字符的时候可能会发生截
>断。在类的外部,或类名只包含下划线时,不会发生截断。
>
>我看得迷糊了。什么叫做“有限的支持”呢?我应该怎样在一个
>类里定义一个私有变量呢?是不是按照上面说得以两个下划线开
>头?
>
> --- limodou <chatme at 263.net> 的正文:
>> mike gaul,您好!
>> 
>> 
>>
>一个下划线一般是私有变量(只是一个约定),两个下划线一般表示特殊含义。在python编程规范中有介绍。PEP
>>
>8。在http://blog.csdn.net/dreamingk/archive/2004/07/26/51667.aspx有中文的链接。
>> 
>> ======= 2004-08-09 15:46:50 您在来信中写道:=======
>> 
>> >那么以一个下划线和两个下划线开头的有什么区别?
>> >我看为什么有以两个下划线开头的呢?
>> >有的前后都有两个下划线。
>> >
>> >不好意思。基础问题。
>> >
>>
>>_________________________________________________________
>> >Do You Yahoo!?
>> >150万曲MP3疯狂搜,带您闯入音乐殿堂
>> >http://music.yisou.com/
>> >美女明星应有尽有,搜遍美图、艳图和酷图
>> >http://image.yisou.com
>> >1G就是1000兆,雅虎电邮自助扩容!
>>
>>http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
>> >_______________________________________________
>> >python-chinese list
>> >python-chinese at lists.python.cn
>> >http://python.cn/mailman/listinfo/python-chinese
>> >
>> 
>> = = = = = = = = = = = = = = = = = = = =
>> 			
>> 
>>         致
>> 礼!
>>  
>> 				 
>>         limodou
>>         chatme at 263.net
>>           2004-08-09
>> 
>> > _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>>  
>
>_________________________________________________________
>Do You Yahoo!?
>150万曲MP3疯狂搜,带您闯入音乐殿堂
>http://music.yisou.com/
>美女明星应有尽有,搜遍美图、艳图和酷图
>http://image.yisou.com
>1G就是1000兆,雅虎电邮自助扩容!
>http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-09


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月09日 星期一 20:57

limodou chatme at 263.net
Mon Aug 9 20:57:43 HKT 2004

mike gaul,您好!

	from Products.PageTemplates.PageTemplateFile import PageTemplateFile
	manage_addIssueTrackerForm = PageTemplateFile('zpt/addIssueTrackerForm', globals())

这是从别的地方抄来的,自已对着api看吧。
======= 2004-08-09 17:12:22 您在来信中写道:=======

>是啊是啊。所以我想用zpt的话,应该怎样Import?
> --- limodou <chatme at 263.net> 的正文:
>> [Zoom.Quiet],您好!
>> 
>> 	zpt与DTML不同,是另一种模板系统。
>> 
>> ======= 2004-08-09 16:46:56 您在来信中写道:=======
>> 
>> >Hollo limodou:
>> >
>> >  但是在 Zope 的处理对象中,都是DHTML好象是也乎,
>> >并不是专指 DHTML 语法的??
>> >
>> >
>> >/******** [2004-08-09]16:46:13 ; limodou wrote:
>> >
>> >limodou> mike gaul,您好!
>> >
>> >limodou> 
>> 是说的zpt吧。找找别人使用zpt的product看一看就行了。
>> >
>> >limodou> ======= 2004-08-09 15:11:36
>> 您在来信中写道:=======
>> >
>> >>>???好象dtml和template page不是一个东西吧。
>> >>>好象语法不同吧。
>> >>>
>> >>> --- "[Zoom.Quiet]" <zoomq at itcase.com> 的正文:
>> >>>> Hollo mike:
>> >>>>  from Globals import DTMLFile
>> >>>> 就是模板文件是也乎??
>> >>>>   
>> >>>> 
>> >>>> 
>> >>>> /******** [2004-08-09]14:32:25 ; mike wrote:
>> >>>> 
>> >>>> mike gaul>
>> >>>> 在zope创建一个product中,可以有这样一句话:
>> >>>> mike gaul> from Globals import HTMLFile
>> >>>> mike gaul> 或者
>> >>>> mike gaul> from Globals import DTMLFile
>> >>>> 
>> >>>> mike gaul>
>> 那我我想,怎样载入一个模板文件template
>> >>>> page呢?
>> >>>> 
>> >>>> mike gaul>
>> >>>>
>>
>>>>_________________________________________________________
>> >>>> mike gaul> Do You Yahoo!?
>> >>>> mike gaul> 150万曲MP3疯狂搜,带您闯入音乐殿堂
>> >>>> mike gaul> http://music.yisou.com/
>> >>>> mike gaul>
>> 美女明星应有尽有,搜遍美图、艳图和酷图
>> >>>> mike gaul> http://image.yisou.com
>> >>>> mike gaul> 1G就是1000兆,雅虎电邮自助扩容!
>> >>>> mike gaul>
>> >>>>
>>
>>>>http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
>> >>>> mike gaul>
>> >>>> _______________________________________________
>> >>>> mike gaul> python-chinese list
>> >>>> mike gaul> python-chinese at lists.python.cn
>> >>>> mike gaul>
>> >>>>
>> http://python.cn/mailman/listinfo/python-chinese
>> >>>> 
>> >>>> 
>> >>>> ********************************************/
>> >>>> 
>> >>>> -- 
>> >>>> Free as in Freedom
>> >>>> 
>> >>>>  [Zoom.Quiet]                           
>> >>>> 
>> >>>> #=========================================#
>> >>>> ]Time is unimportant, only life important![
>> >>>> #=========================================#
>> >>>> 
>> >>>> sender is the Bat!2.12.00
>> >>>> 
>> >>>> _______________________________________________
>> >>>> python-chinese list
>> >>>> python-chinese at lists.python.cn
>> >>>>
>> http://python.cn/mailman/listinfo/python-chinese
>> >>>>  
>> >>>
>>
>>>>_________________________________________________________
>> >>>Do You Yahoo!?
>> >>>美女明星应有尽有,"一搜"搜遍美图、艳图和酷图
>> >>>http://image.yisou.com
>> >>> 
>> >>>100兆邮箱够不够用?雅虎电邮自助扩容!
>>
>>>>http://cn.rd.yahoo.com/mail_cn/tag/100m/*http://cn.promo.yahoo.com/minisite/100m/
>> >>>_______________________________________________
>> >>>python-chinese list
>> >>>python-chinese at lists.python.cn
>> >>>http://python.cn/mailman/listinfo/python-chinese
>> >>>
>> >
>> >limodou> = = = = = = = = = = = = = = = = = = = =
>> >			
>> >
>> >limodou>         致
>> >limodou> 礼!
>> > 
>> >				 
>> >limodou>         limodou
>> >limodou>         chatme at 263.net
>> >limodou>           2004-08-09
>> >
>> >
>> >
>> >********************************************/
>> >
>> >-- 
>> >Free as in Freedom
>> >
>> > [Zoom.Quiet]                           
>> >
>> >#=========================================#
>> >]Time is unimportant, only life important![
>> >#=========================================#
>> >
>> >sender is the Bat!2.12.00
>> >
>> >_______________________________________________
>> >python-chinese list
>> >python-chinese at lists.python.cn
>> >http://python.cn/mailman/listinfo/python-chinese
>> >
>> 
>> = = = = = = = = = = = = = = = = = = = =
>> 			
>> 
>>         致
>> 礼!
>>  
>> 				 
>>         limodou
>>         chatme at 263.net
>>           2004-08-09
>> 
>> > _______________________________________________
>> python-chinese list
>> python-chinese at lists.python.cn
>> http://python.cn/mailman/listinfo/python-chinese
>>  
>
>_________________________________________________________
>Do You Yahoo!?
>150万曲MP3疯狂搜,带您闯入音乐殿堂
>http://music.yisou.com/
>美女明星应有尽有,搜遍美图、艳图和酷图
>http://image.yisou.com
>1G就是1000兆,雅虎电邮自助扩容!
>http://cn.rd.yahoo.com/mail_cn/tag/1g/*http://cn.mail.yahoo.com/event/mail_1g/
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-09


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月09日 星期一 20:57

limodou chatme at 263.net
Mon Aug 9 20:57:44 HKT 2004

杨元杰,您好!

	首先每接到一个连接请求会生成一个新的connection,这是不一样的。新的与旧的不一样。线程启动时会将connection传入,但以后就不会再传入了。你看到的启动多次,但那是不同的线程,旧的线程不受影响。新的线程传入的又是新的 connection对象,因此没有关系。

    所谓shared global memory那一定是指得同一个对象。你可以试着在每个线程开始处加入id(connection),看一看是否是同一个值。如果不是的话,就验证了每个线程得到的不是同一个connection对象,因此是不相冲突的。但如果是一个值的话,说明引用的是一个对象,这样connection就是shared global memroy了,这样就会冲突。试一试就知道是哪种情况了。

======= 2004-08-09 20:36:59 您在来信中写道:=======

>
>limodou.你好
>  为什么connection不会改变呢? thread.start_new(handleClient, (connection,))中传递不是connection的引用嘛?connection不属于线程的Shared global memory 吗?
>  ps:再请教一下一个process启动了若干线程,那么它的Shared global memory 是什么?是不是这个process的中定义的变量,生成的实例?
>
>
>
>
>> 杨元杰,您好!
>> 
>> 	每次connection都是一个新值。而线程A中的connection你并没有去改变,因此它永远使用的是旧的值。
>> 
>> ======= 2004-08-09 16:00:54 您在来信中写道:=======
>> 
>> >大家好!
>> >这是《programming python》10。4中的一个例子。这例子运行在服务器端,响应客户端的请求
>> >Example 10-7. PP2E\Internet\Sockets\thread-server.py 
>> >#########################################################
>> ># Server side: open a socket on a port, listen for
>> ># a message from a client, and send an echo reply; 
>> ># echos lines until eof when client closes socket;
>> ># spawns a thread to handle each client connection;
>> ># threads share global memory space with main thread;
>> ># this is more portable than fork--not yet on Windows;
>> >#########################################################
>> >
>> >import thread, time
>> >from socket import *                     # get socket constructor and constants
>> >myHost = ''                              # server machine, '' means local host
>> >myPort = 50007                           # listen on a non-reserved port number
>> >
>> >sockobj = socket(AF_INET, SOCK_STREAM)           # make a TCP socket object
>> >sockobj.bind((myHost, myPort))                   # bind it to server port number
>> >sockobj.listen(5)                                # allow up to 5 pending connects
>> >
>> >def now(): 
>> >    return time.ctime(time.time())               # current time on the server
>> >
>> >def handleClient(connection):                    # in spawned thread: reply
>> >    time.sleep(5)                                # simulate a blocking activity
>> >    while 1:                                     # read, write a client socket
>> >        data = connection.recv(1024)
>> >        if not data: break
>> >        connection.send('Echo=>%s at %s' % (data, now()))
>> >    connection.close() 
>> >
>> >def dispatcher():                                # listen until process killd
>> >    while 1:                                     # wait for next connection,
>> >        connection, address = sockobj.accept()   # pass to thread for service
>> >        print 'Server connected by', address,
>> >        print 'at', now() 
>> >        thread.start_new(handleClient, (connection,))
>> >
>> >dispatcher()
>> >
>> >我不明白的是:
>> >    在函数dispatcher()中,当服务器接到一个请求,启动了一个线程A,在线程A运行过程中,服务器又街道另个client的请求,执行connection, address = sockobj.accept(),那么线程A中的connecion不是就会被覆盖了吗?
>> >
>> >
>> >
>> >
>> >
>> >-------------------------------------------------------------------------------------
>> >15寸液晶双子星,震天雷II--D451¥2466/D353¥2399 http://design.163.com/aocmonitor/index.html
>> >_______________________________________________
>> >python-chinese list
>> >python-chinese at lists.python.cn
>> >http://python.cn/mailman/listinfo/python-chinese
>> >
>> 
>> = = = = = = = = = = = = = = = = = = = =
>> 			
>> 
>>         致
>> 礼!
>>  
>> 				 
>>         limodou
>>         chatme at 263.net
>>           2004-08-09
>> 
>> 
>
>-------------------------------------------------------------------------------------
>15寸液晶双子星,震天雷II--D451¥2466/D353¥2399 http://design.163.com/aocmonitor/index.html_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-09


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月09日 星期一 21:04

詹建 flysmart at 163.com
Mon Aug 9 21:04:05 HKT 2004

阅读 一切从游戏开始

抄书:
def calc(seq):
 maximum = 0
 max_item = []

 for i in seq:
  product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
  if product > maximum:
    maximum = product
    max_item = i
  elif product == maximum:
    max_item += ','+i
    return max_item, maximum

seq = [ [5,6,7,8,9], [5,6,7,9,8] ]
max_item,maximum = calc(seq)
print "Maximum at", max_item, ",product", maximum


命令行下:C:\pylearn\first-step>python 1.py 
Traceback (most recent call last):
  File "1.py", line 15, in ?
    max_item,maximum = calc(seq)
TypeError: unpack non-sequence

这是怎么回事情?!

                          2004-08-09

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月09日 星期一 21:11

limodou chatme at 263.net
Mon Aug 9 21:11:06 HKT 2004

詹建,您好!

	看程序calc应该返回一个二元组的tuple,但错误提示说返回的不是tuple.好象calc最后的return 缩近有问题,应该提前一个缩近位。还有一个测试的方法,就是在calc调用处,直接将结果先打出来看一看,看是不是一个二元组的tuple。

   print calc(seq)

======= 2004-08-09 21:04:05 您在来信中写道:=======

>阅读 一切从游戏开始
>
>抄书:
>def calc(seq):
> maximum = 0
> max_item = []
>
> for i in seq:
>  product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
>  if product > maximum:
>    maximum = product
>    max_item = i
>  elif product == maximum:
>    max_item += ','+i
>    return max_item, maximum
>
>seq = [ [5,6,7,8,9], [5,6,7,9,8] ]
>max_item,maximum = calc(seq)
>print "Maximum at", max_item, ",product", maximum
>
>
>命令行下:C:\pylearn\first-step>python 1.py 
>Traceback (most recent call last):
>  File "1.py", line 15, in ?
>    max_item,maximum = calc(seq)
>TypeError: unpack non-sequence
>
>这是怎么回事情?!
>
>                          2004-08-09
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        limodou
        chatme at 263.net
          2004-08-09


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2004年08月09日 星期一 21:20

詹建 flysmart at 163.com
Mon Aug 9 21:20:02 HKT 2004

limodou,您好!

	3KU  非常谢谢 是缩近有问题

======= 2004-08-09 21:11:06 您在来信中写道:=======

>詹建,您好!
>
>	看程序calc应该返回一个二元组的tuple,但错误提示说返回的不是tuple.好象calc最后的return 缩近有问题,应该提前一个缩近位。还有一个测试的方法,就是在calc调用处,直接将结果先打出来看一看,看是不是一个二元组的tuple。
>
>   print calc(seq)
>
>======= 2004-08-09 21:04:05 您在来信中写道:=======
>
>>阅读 一切从游戏开始
>>
>>抄书:
>>def calc(seq):
>> maximum = 0
>> max_item = []
>>
>> for i in seq:
>>  product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
>>  if product > maximum:
>>    maximum = product
>>    max_item = i
>>  elif product == maximum:
>>    max_item += ','+i
>>    return max_item, maximum
>>
>>seq = [ [5,6,7,8,9], [5,6,7,9,8] ]
>>max_item,maximum = calc(seq)
>>print "Maximum at", max_item, ",product", maximum
>>
>>
>>命令行下:C:\pylearn\first-step>python 1.py 
>>Traceback (most recent call last):
>>  File "1.py", line 15, in ?
>>    max_item,maximum = calc(seq)
>>TypeError: unpack non-sequence
>>
>>这是怎么回事情?!
>>
>>                          2004-08-09
>>_______________________________________________
>>python-chinese list
>>python-chinese at lists.python.cn
>>http://python.cn/mailman/listinfo/python-chinese
>>
>
>= = = = = = = = = = = = = = = = = = = =
>			
>
>        致
>礼!
> 
>				 
>        limodou
>        chatme at 263.net
>          2004-08-09
>
>_______________________________________________
>python-chinese list
>python-chinese at lists.python.cn
>http://python.cn/mailman/listinfo/python-chinese
>

= = = = = = = = = = = = = = = = = = = =
			

        致
礼!
 
				 
        詹建
        flysmart at 163.com
          2004-08-09


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2024

    京ICP备05028076号