Python论坛  - 讨论区

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

2004年08月09日 星期一 16:00

杨元杰 feynman1983 at 163.com
Mon Aug 9 16:00:54 HKT 2004

大家好!
这是《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

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

2004年08月09日 星期一 16:40

limodou chatme at 263.net
Mon Aug 9 16:40:05 HKT 2004

HD,您好!

	开放了没有,让我们也试试呀。

======= 2004-08-09 13:20:33 您在来信中写道:=======

>呵呵,给社区用呀 :)
>
>On Mon, 9 Aug 2004 13:15:12 +0800, limodou <chatme at 263.net> wrote:
>> HD,您好!
>> 
>>         没地用啊。要自已装吗?
>> 
>> ======= 2004-08-09 13:13:39 您在来信中写道:=======
>> 
>> >有人装好过它的comments插件吗? :(
>> >我受不了了,这东东玩不起来了。
>> >--
>> >HD(燃烧中的火)
>> >我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。
>> >_______________________________________________
>> >python-chinese list
>> >python-chinese at lists.python.cn
>> >http://python.cn/mailman/listinfo/python-chinese
>> >
>> 
>> = = = = = = = = = = = = = = = = = = = =
>> 
>>>> 礼!
>> 
>> 
>> limodou
>> chatme at 263.net
>> 2004-08-09
>> 
>> 
>> 
>> 
>
>
>-- 
>HD(燃烧中的火)
>我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。
>_______________________________________________
>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日 星期一 16:41

limodou chatme at 263.net
Mon Aug 9 16:41:07 HKT 2004

Zoom.Quiet,您好!

	现在还有许多python的blog系统。

======= 2004-08-09 13:34:25 您在来信中写道:=======

>Hollo HD:
>
>  其主站点的wiki 中有专门的如何安装的说明,看起来是超级麻烦的东西………………
>
>Python 的 Blog 除了Zope 平台中的,好的只有这个了嘛????
>
>
>/******** [2004-08-09]13:33:20 ; HD wrote:
>
>HD> 有人装好过它的comments插件吗? :(
>HD> 我受不了了,这东东玩不起来了。
>
>
>********************************************/
>
>-- 
>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


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

2004年08月09日 星期一 16:42

limodou chatme at 263.net
Mon Aug 9 16:42:36 HKT 2004

mike gaul,您好!

	是说的zpt吧。找找别人使用zpt的product看一看就行了。

======= 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
        chatme at 263.net
          2004-08-09


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

2004年08月09日 星期一 16:44

Dreamingk(天成) dreamingk at tom.com
Mon Aug 9 16:44:12 HKT 2004

limodou,您好!

	wiki 早就开放了,现在正在编辑文章中。不过这个wiki的主页面没有个子目录的索引,所以你找不到其他的页面,你可以通过RecentChanges 来了解有那些内容!

======= 2004-08-09 16:40:05 您在来信中写道:=======

>HD,您好!
>
>	开放了没有,让我们也试试呀。
>
>======= 2004-08-09 13:20:33 您在来信中写道:=======
>
>>呵呵,给社区用呀 :)
>>
>>On Mon, 9 Aug 2004 13:15:12 +0800, limodou <chatme at 263.net> wrote:
>>> HD,您好!
>>> 
>>>         没地用啊。要自已装吗?
>>> 
>>> ======= 2004-08-09 13:13:39 您在来信中写道:=======
>>> 
>>> >有人装好过它的comments插件吗? :(
>>> >我受不了了,这东东玩不起来了。
>>> >--
>>> >HD(燃烧中的火)
>>> >我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。
>>> >_______________________________________________
>>> >python-chinese list
>>> >python-chinese at lists.python.cn
>>> >http://python.cn/mailman/listinfo/python-chinese
>>> >
>>> 
>>> = = = = = = = = = = = = = = = = = = = =
>>> 
>>>>>> 礼!
>>> 
>>> 
>>> limodou
>>> chatme at 263.net
>>> 2004-08-09
>>> 
>>> 
>>> 
>>> 
>>
>>
>>-- 
>>HD(燃烧中的火)
>>我工作我快乐,我勤奋我收获。请与我一起快乐,与我一起收获。
>>_______________________________________________
>>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
>

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

        致
礼!
 
				 
        Dreamingk(天成)
        dreamingk at tom.com
          2004-08-09


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

2004年08月09日 星期一 16:45

limodou chatme at 263.net
Mon Aug 9 16:45:40 HKT 2004

杨元杰,您好!

	每次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


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

2004年08月10日 星期二 04:10

c[Box] cbox_wu at hotmail.com
Tue Aug 10 04:10:16 HKT 2004

没错,覆盖了,不过连接对象已经传导线程里



-----邮件原件-----
发件人: python-chinese-bounces at lists.python.cn
[mailto:python-chinese-bounces at lists.python.cn] 代表 杨元杰
发送时间: 2004年8月9日 16:01
收件人: python-chinese at lists.python.cn
主题: [python-chinese] 请教一个关于线程的问题


大家好!
这是《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()
        print id(connection), id(address)
        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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20040810/aa311b76/attachment.html

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号