V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  dbow  ›  全部回复第 20 页 / 共 24 页
回复总数  477
1 ... 12  13  14  15  16  17  18  19  20  21 ... 24  
2017-08-15 17:21:22 +08:00
回复了 dbow 创建的主题 随想 我现在有点不敢去测智商。
@jason2017
@580a388da131
同行们, 咱们内部交流, 就别上战忽局那一套了, 要真诚。
2017-08-15 17:12:56 +08:00
回复了 dbow 创建的主题 随想 我现在有点不敢去测智商。
@BoBoy 我带一个小团队
2017-08-15 16:36:46 +08:00
回复了 dbow 创建的主题 随想 我现在有点不敢去测智商。
@sharkli 没钱上车, 不是不想上。
2017-08-15 16:33:31 +08:00
回复了 dbow 创建的主题 随想 我现在有点不敢去测智商。
@Hozzz 杭州, 南京
2017-08-15 15:26:15 +08:00
回复了 dbow 创建的主题 随想 我现在有点不敢去测智商。
@Livid 发个贴, 在信息流里看不到是什么情况?
<br>
这个贴的列表显示时间也是错的。
<br>
本贴:dbow · 1 小时 33 分钟前 ·
<br>
列表显示:dbow • 5 小时 37 分钟前
2017-07-12 17:24:30 +08:00
回复了 bakabie 创建的主题 Python 请问下, Python 如何做到结构体内嵌结构体呢?
https://docs.python.org/2/library/ctypes.html#structures-and-unions

You can, however, build much more complicated structures. A structure can itself contain other structures by using a structure as a field type.

Here is a RECT structure which contains two POINTs named upperleft and lowerright:

>>>
>>> class RECT(Structure):
... _fields_ = [("upperleft", POINT),
... ("lowerright", POINT)]
...
>>> rc = RECT(point)
>>> print rc.upperleft.x, rc.upperleft.y
0 5
>>> print rc.lowerright.x, rc.lowerright.y
0 0
>>>
Nested structures can also be initialized in the constructor in several ways:

>>>
>>> r = RECT(POINT(1, 2), POINT(3, 4))
>>> r = RECT((1, 2), (3, 4))
2017-07-03 18:16:52 +08:00
回复了 raycool 创建的主题 Python 咨询个 Python 多进程中出现的奇怪问题
@raycool
@wwqgtxx

这个是忘掉处理 apply_async 的结果造成的误解
apply_async -> result -> result.get() 就能看到异常了, 传递的东西得能 pickle 序列化

Traceback (most recent call last):
File "test.py", line 36, in <module>
print result.get()
File "/usr/lib/python2.7/multiprocessing/pool.py", line 567, in get
raise self._value
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed

想要序列化 class instance, 可以看看 pickle 的文档
https://docs.python.org/2/library/pickle.html
-> Pickling and unpickling normal class instances
2017-07-01 10:47:45 +08:00
回复了 yucongo 创建的主题 Python Windows 下 Python 3 出 segfault 时如何找错?
gdb --args ./app ..... 跑一遍, 出了 segment fault, 用 bt 命令, 输出 stack trace
@wwqgtxx ctypes 调用 c 函数是自动释放 GIL 的, 除非是调用 python 自己的 C API.

821 #ifdef WITH_THREAD
822 if ((flags & FUNCFLAG_PYTHONAPI) == 0)
823 Py_UNBLOCK_THREADS
824 #endif
825 if (flags & FUNCFLAG_USE_ERRNO) {
826 int temp = space[0];
827 space[0] = errno;
828 errno = temp;
829 }
830 #ifdef MS_WIN32
831 if (flags & FUNCFLAG_USE_LASTERROR) {
832 int temp = space[1];
833 space[1] = GetLastError();
834 SetLastError(temp);
835 }
2017-07-01 10:34:39 +08:00
回复了 yucongo 创建的主题 Python Windows 下 Python 3 出 segfault 时如何找错?
完整的错误信息贴出来看看。
@wwqgtxx 是的, 主动释放的, 方法就是下面的, 在底层 C 代码释放的。
Py_BEGIN_ALLOW_THREADS
rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE);
Py_END_ALLOW_THREADS
@davinci 释放不了, 需要主动写代码释放, 典型的模式是在阻塞之前释放线程锁, 阻塞操作结束后重新取得。
以 time.sleep()里的这段代码为例,
Py_BEGIN_ALLOW_THREADS
rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE);
Py_END_ALLOW_THREADS
释放不了, 需要主动写代码释放, 典型的模式是在阻塞之前释放线程锁, 阻塞操作结束后重新取得。
以 time.sleep()里的这段代码为例,
Py_BEGIN_ALLOW_THREADS
rc = WaitForSingleObjectEx(hInterruptEvent, ul_millis, FALSE);
Py_END_ALLOW_THREADS
要不试试我的库 https://github.com/maliubiao/simple_http
还能控制并发量, 超时时间
### 异步方式
```shell

In [21]: def print_it(x):
import pprint
....: pprint.pprint(x)
....:

In [22]: async_http.repeat_tasks([{"url": "http://www.baidu.com", "parser": print_it}])
{'chain': None,
'chain_idx': 0,
'con': <socket._socketobject object at 0x2812bb0>,
'fd': 5,
'header_only': False,
'parser': <function print_it at 0x283da28>,
'proxy': '',
'random': '60804c2a0b053fbd',
'recv': <cStringIO.StringO object at 0x283a3e8>,
'redirect': 0,
'res_cookie': {'BAIDUID': {'domain': '.baidu.com',
'expires': 'Thu, 31-Dec-37 23:55:55 GMT',
'max-age': '2147483647',
'path': '/',
'value': 'BCB0BBBB4312D00C88BCDC9EEAAE3726:FG=1'},
'BD_LAST_QID': {'Max-Age': '1',
'path': '/',
'value': '16069052107084303783'},
'BIDUPSID': {'domain': '.baidu.com',
'expires': 'Thu, 31-Dec-37 23:55:55 GMT',
'max-age': '2147483647',
'path': '/',
'value': 'BCB0BBBB4312D00C88BCDC9EEAAE3726'}},
'res_header': {'Connection': 'Keep-Alive',
'Content-Length': '215',
'Content-Type': 'text/html',
'Date': 'Thu, 21 May 2015 15:50:43 GMT',
'Location': 'https://www.baidu.com/',
'P3P': 'CP=" OTI DSP COR IVA OUR IND COM "',
'Server': 'BWS/1.1',
'Set-Cookie': 'BAIDUID=BCB0BBBB4312D00C88BCDC9EEAAE3726:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com\r\nBIDUPSID=BCB0BBBB4312D00C88BCDC9EEAAE3726; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com\r\nBD_LAST_QID=16069052107084303783; path=/; Max-Age=1',
'X-UA-Compatible': 'IE=Edge,chrome=1'},
'res_status': {'message': 'Moved Temporarily',
'protocol': 'HTTP/1.1',
'status': 302},
'retry': 0,
'send': <cStringIO.StringO object at 0x25fb8f0>,
'ssl': False,
'start': 1432223278.489937,
'status': 512,
'text': '<html>\r\n<head><title>302 Found</title></head>\r\n<body bgcolor="white">\r\n<center><h1>302 Found</h1></center>\r\n<hr><center>pr-nginx_1-0-221_BRANCH Branch\nTime : Wed May 20 10:35:46 CST 2015</center>\r\n</body>\r\n</html>\r\n',
'url': 'http://www.baidu.com'}
async_http Thu May 21 23:47:58 2015: 'acnt: 1, fcnt: 0, time: 0'
```
要是刚学 python, 会 C 的话 , 建议直接文档对着源代码看, 免得瞎猜, 浪费时间。https://github.com/python/cpython/blob/master/Python/ceval.c#L1103
这样就应该写对了, 多线程抢占的程序的结果比较随机, lock 并没有线程优先取得的问题。
另外 bytecode 的执行 应该是 100 个为单位切换执行
The interpreter releases the GIL every 100 "ticks".
661 /* for manipulating the thread switch and periodic "stuff" - used to be
662 per thread, now just a pair o' globals */
663 int _Py_CheckInterval = 100;
改成这样, 应该就是顺序输出 了. 按你的写法哪个先 acuire lock 不一定。
def run_thread(n):
with lock:
code
CPython 有 GIL, 代码是单线程执行的, run_thread 一个线程执行完, 另一个线程才能执行, 跟锁没关系。
2015-12-11 10:04:17 +08:00
回复了 dbow 创建的主题 程序员 golang 经验交流, 如何避免 gc 杀手
@CRVV 我讲如果能处理
enter function
new slice
leave function
问题就小很多
在 while 里引用了别人写的这种函数就是 for {malloc}, 给 gc 集中式回收造成了很大的压力
如果能实现把 function 内显而易见的 heap 分配可以立刻回收掉, 这种问题就能解决掉, 内存占用不会升的太快, gc 造成的延迟也会减小。
这事不在内存池上, 因为有了 gc , 别人的代码会"不负责任的"这么干.
2015-12-10 12:55:59 +08:00
回复了 dbow 创建的主题 程序员 golang 经验交流, 如何避免 gc 杀手
@tiancaiamao
问题是 f 多数情况下不是自己写的, 所以要求 gc 能自己搞定这个事。
1 ... 12  13  14  15  16  17  18  19  20  21 ... 24  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2825 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 25ms · UTC 15:42 · PVG 23:42 · LAX 08:42 · JFK 11:42
Developed with CodeLauncher
♥ Do have faith in what you're doing.