PHP 8.2.30
Preview: threadsafe.py Size: 772 B
/proc/thread-self/root/opt/cloudlinux/venv/lib/python3.11/site-packages/pyvirtualdisplay/examples/threadsafe.py

"Start Xvfb server and open xmessage window. Thread safe."

import threading

from easyprocess import EasyProcess

from pyvirtualdisplay.smartdisplay import SmartDisplay


def thread_function(index):
    # manage_global_env=False is thread safe
    with SmartDisplay(manage_global_env=False) as disp:
        cmd = ["xmessage", str(index)]
        # disp.new_display_var should be used for new processes
        # disp.env() copies global os.environ and adds disp.new_display_var
        with EasyProcess(cmd, env=disp.env()):
            img = disp.waitgrab()
            img.save("xmessage{}.png".format(index))


t1 = threading.Thread(target=thread_function, args=(1,))
t2 = threading.Thread(target=thread_function, args=(2,))
t1.start()
t2.start()
t1.join()
t2.join()

Directory Contents

Dirs: 1 × Files: 7

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-06 08:01:08
Edit Download
245 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
266 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
736 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
589 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
772 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
284 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download
0 B lrw-r--r-- 2026-01-20 13:01:47
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).