V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  petelin  ›  全部回复第 30 页 / 共 36 页
回复总数  713
1 ... 22  23  24  25  26  27  28  29  30  31 ... 36  
@petelin 说的有点问题, 并不是主线程等待子线程都完成在退出(那是 join 干的事), 而是 python 解释器保证 当没有活跃的进程之后才退出(所以主线程先退出, 子线程继续执行, 知道完成 python 进程退出是有可能的).
那么这段代码问题是, 主进程跑完之后, 所有子进程都被干掉了.无论你子进程在干什么, 直接被干掉.

所有需要在主进程加一段代码
pool.close()
pool.join()

这样看起来就完了, 主进程会等待子进程完成, 而子进程会等待线程完成

可是子进程不会等待线程完成, 所以
t.start()
t.join()

那么为什么子进程不会等待非守护线程结束呢??? 我去学习一个
@justou 后台线程作用是, 当主线程退出的时候, 不用管他们(那他们能欢快的在后台运行吗? 看进程, 进程都退出了,直接被干掉). 反而非后台进程, 主进程即使不 join, 退出之前也要等子线程结束.

所以还是要学习一个.
2017 年 9 月 27 日
回复了 im67 创建的主题 Python py3 学习 socket 中约到的问题
def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self): def handler(self):

浪费 绳命
2017 年 9 月 20 日
回复了 cevincheung 创建的主题 Python Python3 怎么格式化时间(时区问题)
时间的比较应该在同一个时区下. 还有 replace 有坑, 使用 tzc.localize 更好
2017 年 9 月 20 日
回复了 cevincheung 创建的主题 Python Python3 怎么格式化时间(时区问题)
In [24]: from datetime import datetime
...: from pytz import timezone
...: import pytz
...: tzc = timezone('PRC')
...: tzu = timezone('US/Hawaii')
...:
...: time = datetime.utcnow()
...:
...: print('C:'+time.replace(tzinfo=tzc).astimezone(tz=pytz.utc).strftime('%H'))
...: print('U:'+time.replace(tzinfo=tzu).astimezone(tz=pytz.utc).strftime('%H'))
...:
C:04
U:23
2017 年 9 月 20 日
回复了 AnonymousAccout 创建的主题 MySQL 如何在 Mysql 中创建一个带条件的唯一索引?
有一种比较猥琐的方法, 在加多 3 个列, b1, c1, d1, 建索引 (a, b1, c1, d1).... 然后不需要唯一索引的写的时候用随机, 需要的正常写业务数据,,,, (我是把 a 当做条件列了)
2017 年 9 月 14 日
回复了 xylsmmmmmmmm3 创建的主题 Python 用 str 转成字符串和 join 转成字符串有什么区别?
a['a'] 是个列表, str 的时候,就是那么打印的

''.join(a['a']) .........

算了我懒得吐槽了..... 两个方法都不一样. 为什么你会认为输出一定要一样?
2017 年 9 月 14 日
回复了 petelin 创建的主题 问与答 请教数据库建立索引
@mm163 我举个例子吧, 如果有 100 万个 status 为 0 的记录, 有一条 stattus 为 1 的记录, 那么查找 status 为 1 的记录能不能直接命中呢? 如果能的话, 那性能提升就很明显.
2017 年 9 月 4 日
回复了 rogwan 创建的主题 Python Python3 写入中文(utf-8)到文本,还是会出现乱码?
乱码贴出来看看
2017 年 8 月 22 日
回复了 hiboshi 创建的主题 问与答 Python 如何实现这样的数据结构
In [73]: for k,g in groupby(aa, lambda x:x['id']):
...: tmp[k] = '|'.join([item['txt'] for item in list(g)]) + '|'
2017 年 8 月 22 日
回复了 hiboshi 创建的主题 问与答 Python 如何实现这样的数据结构
In [22]: for item in aa:
...: tmp[item['id']]= tmp.get(item['id'], '') + item['txt'] + '|'
2017 年 8 月 16 日
回复了 petelin 创建的主题 Python 有什么好办法约束一个函数的执行时间吗?
@lolizeppelin 不行。
2017 年 8 月 15 日
回复了 petelin 创建的主题 Python 有什么好办法约束一个函数的执行时间吗?
```
def run_with_limited_time(func, args, kwargs, time):
"""Runs a function with time limit
:param func: The function to run
:param args: The functions args, given as tuple
:param kwargs: The functions keywords, given as dict
:param time: The time limit in seconds
:return: True if the function ended successfully. False if it was terminated.
"""
def wrapper(queue, func, *args, **kwargs):
return queue.put(func(*args, **kwargs))
import multiprocessing
q = multiprocessing.Queue(maxsize=1)
func = functools.partial(wrapper, q, func)
p = Process(target=func, args=args, kwargs=kwargs)
p.start()
p.join(time)
if p.is_alive():
p.terminate()
raise TimeoutError('time out!')

return q.get_nowait()
```

看起来是最好的实现了
2017 年 8 月 15 日
回复了 petelin 创建的主题 Python 有什么好办法约束一个函数的执行时间吗?
@HarveyDent 比如有一句 sleep(10), 这句话没法再被拆分了.
2017 年 8 月 14 日
回复了 petelin 创建的主题 Python 有什么好办法约束一个函数的执行时间吗?
创建主题时描述错了,为看到的 v 友说声抱歉, Process 和 Thread 说的乱七八糟的, 已改正.
2017 年 8 月 11 日
回复了 just1 创建的主题 程序员 多线程读写文件的问题
正好遇到类似的问题, 我发现写日志的时候, 多进程并没有出现问题, 原来写小于 PIPE_BUF 是原子的.

http://me.xrange.top/2017/mutiprocess_append_1_file.html
@jiage8866 你能帮我下一个吗?
小米 红米 Note4X 全网通版 4GB+64GB 浅蓝色 移动联通电信 4G 手机 ,蓝色的才有 https://item.jd.com/4325123.html#crumb-wrap
@jiage8866 好,看他方不方便加微信,也谢谢你啦
1 ... 22  23  24  25  26  27  28  29  30  31 ... 36  
关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   1659 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 26ms · UTC 16:26 · PVG 00:26 · LAX 08:26 · JFK 11:26
♥ Do have faith in what you're doing.