wxw520xj
wxw520xj
2019-01-09 16:10

PyCharm-执行程序PermissionError: [WinError 5] 拒绝访问。

  • python

学习廖雪峰的python教程-多进程一章时,在PyCharm中执行进程间通信的示例代码,结果无法得到正确的结果。代码如下:

# 进程间通信

# 写数据进程执行的代码:
def write(q):
    print('Process to write: %s' % os.getpid())
    for value in ['A', 'B', 'C']:
        print('Put %s to queue...' % value)
        q.put(value)
        time.sleep(random.random())

# 读数据进程执行的代码:
def read(q):
    print('Process to read: %s' % os.getpid())
    while True:
        value = q.get(True)
        print('Get %s from queue.' % value)

if __name__=='__main__':
    # 父进程创建Queue,并传给各个子进程:
    q = Queue()
    pw = Process(target=write, args=(q,))
    pr = Process(target=read, args=(q,))
    # 启动子进程pw,写入:
    pw.start()
    # 启动子进程pr,读取:
    pr.start()
    # 等待pw结束:
    pw.join()
    # pr进程里是死循环,无法等待其结束,只能强行终止:
    pr.terminate()

我的PyCharm中执行结果如下:

G:\PycharmProjects\practice\venv\Scripts\python.exe G:/PycharmProjects/practice/process_and_thread.py
Process to write: 9048
Put A to queue...
Process Process-1:
Traceback (most recent call last):
  File "C:\Users\Xujian\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
    self.run()
  File "C:\Users\Xujian\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "G:\PycharmProjects\practice\process_and_thread.py", line 77, in write
    q.put(value)
  File "C:\Users\Xujian\AppData\Local\Programs\Python\Python37\lib\multiprocessing\queues.py", line 82, in put
    if not self._sem.acquire(block, timeout):
PermissionError: [WinError 5] 拒绝访问。

Process finished with exit code 0

而直接在cmd中,使用python命令执行该py文件可以得到正确的结果。

求助问题在哪?
已经尝试重新安装、以管理员运行PyCharm,都无法解决

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答