jackfrued / jackfrued/Python-100-Days

13中的利用Queue类解决进程共享变量的问题

Open
#499 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Jupyter Notebook
Stars
187k
Forks
55.8k
PR merge metrics
No merged PRs in 30d

Description

from multiprocessing import Process, Queue
from time import sleep

def sub_task(q,string):
    while True:
        try:
            q.put(string)
        except Queue.full(q):
            return
        print(string, end='', flush=True)
        sleep(0.01)


def main():
    q = Queue(10)
    Process(target=sub_task, args=(q,'Ping')).start()
    Process(target=sub_task, args=(q,'Pong')).start()


if __name__ == '__main__':
    main()

输出结果是
PingPingPingPingPongPingPongPingPongPing

在原文档中,作者提到"输出'Ping'和'Pong'共十个“这个问题的时候只提供了错误范例,并且只提到了用Queue类可以简单地解决
这份代码是我自己的解决方案,不知道是不是为最简方法。
错误处理部分不知道能否有更简单的处理方法,作为python初学者,对语言还不太熟悉。

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the multiprocessing Queue example included in the issue and compare it with the original documentation passage it discusses. Clarify whether the documentation should explain the expected ten-item output and the Queue.full handling. Done means the example and its explanation are consistent and the intended behavior is unambiguous.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.