小编aro*_*ire的帖子

Python 多处理池队列通信

我正在尝试实现一个由两个并行运行并通过队列进行通信的进程组成的池。

目标是让写入进程使用队列将消息传递给读取进程。

每个进程都在终端上打印反馈以便获得反馈。

这是代码:

#!/usr/bin/env python

import os
import time
import multiprocessing as mp
import Queue

def writer(queue):
    pid = os.getpid()
    for i in range(1,4):
        msg = i
        print "### writer ", pid, " -> ", msg
        queue.put(msg)
        time.sleep(1)
        msg = 'Done'
    print '### '+msg
    queue.put(msg)

def reader(queue):
    pid = os.getpid()
    time.sleep(0.5)
    while True:
        print "--- reader ", pid, " -> ",
        msg = queue.get()
        print msg
        if msg == 'Done':
            break

if __name__ …
Run Code Online (Sandbox Code Playgroud)

python multiprocessing python-2.7

3
推荐指数
1
解决办法
4038
查看次数

为什么 requests.history 不显示所有重定向?

我正在尝试获取一些维基百科页面的重定向,但是从请求中得到的内容.history对我来说有点奇怪。

如果我做:

>>> request = requests.get("https://en.wikipedia.org/wiki/barcelona", allow_redirects=True)
>>> request.url
u'https://en.wikipedia.org/wiki/Barcelona'
>>> request.history
[<Response [301]>]
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,重定向是正确的,并且我得到的 URL 与.history从浏览器访问页面时得到的 URL 相同。

但如果我这样做:

>>> request = requests.get("https://en.wikipedia.org/wiki/Yardymli_Rayon", allow_redirects=True)
>>> request.url
u'https://en.wikipedia.org/wiki/Yardymli_Rayon'
>>> request.history
[]
Run Code Online (Sandbox Code Playgroud)

.history是空的,但在浏览器中我看到 URL 实际上已更改为: https: //en.wikipedia.org/wiki/Yardymli_District

有人知道如何解决吗?

python redirect wikipedia python-requests

1
推荐指数
1
解决办法
1052
查看次数

TypeError - 字符串索引必须是整数,而不是 str

当我打印从MongoDB检索到的数据时,我得到以下输出:

[{"username": "ashish.mishra", "password": "hxMNwFOa", "frequency": "Daily", "name": "Ashish Mishra", "email": "ashish@mail.com"}]
Run Code Online (Sandbox Code Playgroud)

这是我检索它的方法:

user = db.UserData.find()
user = dumps(user)
print user //this is the printed version above
Run Code Online (Sandbox Code Playgroud)

我想访问每个键。我试过:

print user['username']
Run Code Online (Sandbox Code Playgroud)

print user[0]['username']
Run Code Online (Sandbox Code Playgroud)

它给了我错误:

TypeError: string indices must be integers, not str
Run Code Online (Sandbox Code Playgroud)

我知道有很多关于这些的线索,但到目前为止我还没有成功。知道如何做到这一点吗?

python typeerror

-1
推荐指数
1
解决办法
2万
查看次数

如何在Python中置换两个列表

以下是两个列表示例

list = [["mo", "thu"], ["we", "fri"], ["mo", "mo"]]
list2 = [["mo", "fri", "fri"], ["we", "we"]]
Run Code Online (Sandbox Code Playgroud)

所以这些列表是随机排列的.我在这里举了两个例子.我想要做的是计算这些列表可以排列的所有可能方式.在列表[1]*列表[2]*列表[3]的级别上.所有可能的组合.我希望达到的一个较小的例子是:

list3 = [["we", "thu"],["fri", "thu"]]
Run Code Online (Sandbox Code Playgroud)

- >

[["we", "fri"], ["we", "thu"], ["thu", "fri"], ["thu", "thu"]]
Run Code Online (Sandbox Code Playgroud)

此外,列表是随机的,因此列表中的元素数量或嵌套列表可能会有所不同.我可以通过很多代码来解决这个问题,但我希望有一个更简单的方法.

干杯

python

-1
推荐指数
1
解决办法
79
查看次数