ZeroMQ - the fastest way of communication between threads, processes, programming languages, operating systems, networks

Post your working scripts, libraries and tools for AHK v1.1 and older
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

ZeroMQ - the fastest way of communication between threads, processes, programming languages, operating systems, networks

07 Sep 2022, 05:20

What is ZeroMQ ?

In short, it is a universal messaging library that can transfer message between threads, processes, programming languages, operating systems, and networks.

It can transfer any kind of messages, such as number, text, binary, etc., and has no size limit.

It has various modes, such as request/reply, publish/subscribe, push/pull, etc., and can easily implement 1-to-1, 1-to-many, many-to-many and other kinds of transmission.

It is very fast, the AHK version can send 823000 messages per second in 10-byte size in push/pull mode on the CPU R7 5800H.

That's 10+ times faster than SendMessage (which usually only transmits numbers).

The C language version is even faster, estimated to be 10+ times faster than the AHK version.



What can it do ?

For example, communication between threads or processes.

For example, using AHK to write extensions for the editors like SciTE, Sublime Text, VScode. (use ZeroMQ in the editors's official extension language to forward events and variables to AHK)

For example, make a news or weather update service with 10,000 users subscribed to updates at the same time.

It is also possible to make a distributed computing program, or to make a chat software.

Only imagination is limited.



How to learn it ?
1. download Lib and Examples.
2. read and run Getting Started Examples.
3. read ØMQ - The Guide, and read and run Advanced Examples.
4. google.



Download Lib and Examples

https://github.com/telppa/ahkzmq
Last edited by tuzi on 09 Sep 2022, 11:37, edited 1 time in total.
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

Re: ZeroMQ - the fastest way of communication between threads, processes, programming languages, operating systems, netw

07 Sep 2022, 05:26

here is a example to show how easy to use it.

client.ahk

Code: Select all

#NoEnv
SetBatchLines -1                       ; maximum speed

zmq := new ZeroMQ                      ; init ZeroMQ

context := zmq.context()               ; create a context
socket := context.socket(zmq.REQ)      ; create a socket with REQ
socket.connect("tcp://localhost:5555") ; connect to endpoint

loop 10000                             ; send 10000 times
{
  socket.send_string("Hello")          ; send "Hello" to server
  msg := socket.recv_string()          ; receiving response from the server
}

MsgBox Client received reply %msg%

ExitApp                                ; automatic resource release on exit

#Include ZeroMQ.ahk
server.ahk

Code: Select all

#NoEnv
SetBatchLines -1

zmq := new ZeroMQ

context := zmq.context()
socket := context.socket(zmq.REP) ; create a socket with REP
socket.bind("tcp://*:5555")       ; bind to endpoint

loop 10000
{
  msg := socket.recv_string()     ; receiving request from client
  socket.send_string("World")     ; send "World" to client
}

MsgBox Server received request %msg%

ExitApp

#Include ZeroMQ.ahk
Eik
Posts: 9
Joined: 26 Feb 2020, 20:53

Re: ZeroMQ - the fastest way of communication between threads, processes, programming languages, operating systems, netw

09 Sep 2022, 06:39

Thanks tuzi, I like it!
There are many possibilities for ZeroMQ!

I ran ComObj and ZeroMQ, each calling a Python function and then executing the function to get the string.
I have only minimal knowledge of Python, so this is a simple call speed comparison.
com 0.006370
zmq 0.001430

The advantage of zmq is that it can execute Python code with @jit, which com cannot.

Here is the actual code used

Code: Select all

;ahk
ret := py_zmq("test")
;ret := py_zmq("jit_test")
t   := QPC(0)
msgbox,% ret "`n" t
Return ;---------------------


py_zmq(func_arg*){
	zmq     := new ZeroMQ
	context := zmq.context()
	socket  := context.socket(zmq.REQ)
	socket.connect("ipc://aaa")

QPC(1)

	For _, v in func_arg
		socket.send_string(v  ,zmq.SNDMORE,"UTF-8")

	socket.send_string("ahkSendEnd",,"UTF-8")
	Return socket.recv_string(,"UTF-8")
}


QPC( R := 0 ) {    ; By SKAN,  http://goo.gl/nf7O4G,  CD:01/Sep/2014 | MD:01/Sep/2014
  Static P := 0,  F := 0,     Q := DllCall( "QueryPerformanceFrequency", "Int64P",F )
Return ! DllCall( "QueryPerformanceCounter","Int64P",Q ) + ( R ? (P:=Q)/F : (Q-P)/F ) 
}

Code: Select all

#Python
import sys
from numba import jit
import zmq

def func_call(func,*a):
	i = len(a)
	if i==0:
		return globals()[func]()
	elif i==1:
		return globals()[func](a[0])
	elif i==2:
		return globals()[func](a[0],a[1])
	elif i==3:
		return globals()[func](a[0],a[1],a[2])
	elif i==4:
		return globals()[func](a[0],a[1],a[2],a[3])

def ahk_zmq():
	context = zmq.Context()
	socket = context.socket(zmq.REP)
	socket.bind("ipc://aaa")

	i   = 0
	arg = {}

	while 1:
		str = socket.recv_string()
		if i==0:
			fn = str
		elif str=="ahkSendEnd":
			break
		else:
			arg[i] = str
		i += 1

	ret = func_call(fn,*arg)
	socket.send_string(ret)
	#socket.close()
	#context.destroy()
	return

def test():
	return "hello"

@jit
def jit_test():
	res = 0
	for i in range(10000):
		for j in range(10000):
			res += 1
	return str(res)


ahk_zmq()
sys.exit()

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 78 guests