python gevent 解决 (mach-o file, but is an incompatible architecture (have ‘x86_64‘, need ‘arm64‘))问题
Posted kainx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python gevent 解决 (mach-o file, but is an incompatible architecture (have ‘x86_64‘, need ‘arm64‘))问题相关的知识,希望对你有一定的参考价值。
报错
在执行import gevent
时出现下面的错误
ImportError: dlopen(/Users/user/data/code/venv/lib/python3.10/site-packages/gevent/_gevent_c_hub_local.cpython-310-darwin.so, 0x0002): tried: '/Users/liam/code/venv/lib/python3.10/site-packages/gevent/_gevent_c_hub_local.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64')), '/System/Volumes/Preboot/Cryptexes/OS/Users/user/code/venv/lib/python3.10/site-packages/gevent/_gevent_c_hub_local.cpython-310-darwin.so' (no such file), '/Users/user/c'o'd'e/venv/lib/python3.10/site-packages/gevent/_gevent_c_hub_local.cpython-310-darwin.so' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64'))
解决办法
出现该错误是因为我电脑是M1 arm架构的CPU, 需要gevent对arm的支持还有一点问题,有两种解决方式
- 方式1. 切换M1的终端架构
在终端执行arch -x86_64 bash
可以直接切换到x86_64
架构可以解决一些兼容性问题user@user ~ % uname -a Darwin user 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:15:52 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8112 arm64 user@user ~ % arch -x86_64 bash bash-3.2$ uname -a Darwin user 22.1.0 Darwin Kernel Version 22.1.0: Sun Oct 9 20:15:52 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T8112 x86_64
- 方式2. 源码方式编译安装gevent
执行arch -arm64 pip3 --no-cache-dir install gevent gevent
命令可以通过源码编译方式安装兼容arm架构的gevent
如果安装后出现下面的ValueError: greenlet.greenlet size changed
错误, 有可能是greenlet
的版本和gevent
版本不匹配导致的, gevent
似乎因为修某个bug回退了greenlet
的版本
ValueError: greenlet.greenlet size changed, may indicate binary incompatibility. Expected 160 from C header, got 40 from PyObject
我的版本如下:
gevent 22.8.0
greenlet 1.1.3
安装gevent
的命令可以改成 arch -arm64 pip3 --no-cache-dir pip install greenlet==1.1.3 gevent==22.8.0
参考
- https://apple.stackexchange.com/questions/436801/m1-mac-mach-o-file-but-is-an-incompatible-architecture-have-x86-64-need-a
- https://github.com/gevent/gevent/issues/1923
- https://github.com/gevent/gevent/issues/1927
- https://github.com/neovim/pynvim/issues/502
- https://bugzilla.redhat.com/show_bug.cgi?id=1965584
day10-3-协程(gevent)解决socket并发
协程,又称微线程,纤程。英文名Coroutine。gevent是一个基于协程的Python网络库
协程的概念很早就提出来了,但直到最近几年才在某些语言(如Lua)中得到广泛应用。
子程序,或者称为函数,在所有语言中都是层级调用,比如A调用B,B在执行过程中又调用了C,C执行完毕返回,B执行完毕返回,最后是A执行完毕。
所以子程序调用是通过栈实现的,一个线程就是执行一个子程序。
子程序调用总是一个入口,一次返回,调用顺序是明确的。而协程的调用和子程序不同。
协程看上去也是子程序,但执行过程中,在子程序内部可中断,然后转而执行别的子程序,在适当的时候再返回来接着执行。、
客户端:
import socket HOST = ‘localhost‘ # The remote host PORT = 8701 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) while True: msg = bytes(input(">>:"), encoding="utf8") s.sendall(msg) data = s.recv(1024) # print(‘Received‘, data) s.close()
服务器端:
import sys import socket import time import gevent from gevent import socket, monkey monkey.patch_all() def server(port): s = socket.socket() s.bind((‘0.0.0.0‘, port)) s.listen(500) while True: cli, addr = s.accept() gevent.spawn(handle_request, cli) def handle_request(conn): try: while True: data = conn.recv(1024) print("recv:", data) conn.send(data) if not data: conn.shutdown(socket.SHUT_WR) except Exception as ex: print(ex) finally: conn.close() if __name__ == ‘__main__‘: server(8701)
执行效果:
服务器:
recv: b‘47‘
recv: b‘414‘
recv: b‘11‘
recv: b‘2222‘
客户端1:
>>:47
Received b‘47‘
>>:11
Received b‘11‘
客户端2:
>>:414
Received b‘414‘
>>:2222
Received b‘2222‘
以上是关于python gevent 解决 (mach-o file, but is an incompatible architecture (have ‘x86_64‘, need ‘arm64‘))问题的主要内容,如果未能解决你的问题,请参考以下文章
python gevent MonkeyPatchWarning: Monkey-patching ssl after